MongoCollection.distinct

Returns an input range of all unique values for a certain field for records matching the given query.

struct MongoCollection
distinct
(
R = Bson
Q
)
(
string key
,)

Parameters

key string

Name of the field for which to collect unique values

query Q

The query used to select records

Return Value

Type: auto

An input range with items of type R (Bson by default) is returned.

Examples

import std.algorithm : equal;
import vibe.db.mongo.mongo;

void test()
{
	auto db = connectMongoDB("127.0.0.1").getDatabase("test");
	auto coll = db["collection"];

	coll.drop();
	coll.insert(["a": "first", "b": "foo"]);
	coll.insert(["a": "first", "b": "bar"]);
	coll.insert(["a": "first", "b": "bar"]);
	coll.insert(["a": "second", "b": "baz"]);
	coll.insert(["a": "second", "b": "bam"]);

	auto result = coll.distinct!string("b", ["a": "first"]);

	assert(result.equal(["foo", "bar"]));
}

Meta