1

In MongoDB 3.0 with the C# 2.0 driver, how do you get a distinct list of values using DistinctAsync from a document's array of sub documents?

I'm looking for the C# equivalent of this in the shell:

db.cars.distinct("parts.name", {"make":"Ford"})

After admitting defeat, I resorted to this shell-ish code:

var distinctParts = await db.RunCommandAsync<BsonDocument>(new BsonDocument {
    { "distinct", "cars"}, 
    {"key", "parts.name"},
    {"query", new BsonDocument { { "make", "Ford" }} } });

Thanks!

  • 1
    What about `await db.DistinctAsync(...)` is not working for you, and what error(s) does it produce? – Alex Apr 27 '15 at 22:14

1 Answers1

1

Something like this should work:

var filter = new  MongoDB.Driver.ExpressionFilterDefinition<CARS_TYPE>(x => x.make == "ford");
var distinctParts = await cars_collection.DistinctAsync<string>("parts.name", filter);