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
  • 1
    What about await db.DistinctAsync<MyType>(...) is not working for you, and what error(s) does it produce? Commented Apr 27, 2015 at 22:14

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.