vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

Function MongoCollection.distinct

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

auto distinct(R, Q) (
  string fieldName,
  Q query,
  DistinctOptions options = DistinctOptions.init
);

Parameters

NameDescription
fieldName Name of the field for which to collect unique values
query The query used to select records
options Options to apply

Returns

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

Example

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.insertOne(["a": "first", "b": "foo"]);
	coll.insertOne(["a": "first", "b": "bar"]);
	coll.insertOne(["a": "first", "b": "bar"]);
	coll.insertOne(["a": "second", "b": "baz"]);
	coll.insertOne(["a": "second", "b": "bam"]);

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

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

Authors

Sönke Ludwig

Copyright

© 2012-2016 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.