Select Distinct items

476 views
Skip to first unread message

Todd Forsberg

unread,
May 23, 2015, 3:12:04 AM5/23/15
to mongod...@googlegroups.com
I'm trying to use the new C# 2.0 "async" driver to retrieve distinct values only of a particular field from all documents in a collection.  

Can someone give a simple example of how to do this?  

I've searched all over the web and have not found any examples using the new driver.  There are plenty for the legacy driver.

Craig Wilson

unread,
May 29, 2015, 7:17:50 AM5/29/15
to mongod...@googlegroups.com
Hi Todd,

What have you tried? There is a method called DistinctAsync on IMongoCollection.

var client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<BsonDocument>("people");

// get all the distinct values for the "firstName" field.
var distinctItems = collection.DistinctAsync("firstName", new BsonDocument());


Craig
Reply all
Reply to author
Forward
0 new messages