0

Let's assume I have a class like this:

class MyObject
{
    public string Color { get; set; }
    ....
}

and I have a collection of MyObject in MongoDB.

How can I go through the collection and make a list of all the unique colors?

At the end, I'd like a List that contains one entry per color { "yellow", "pink" } for example.

1 Answer 1

0

You want to get the distinct values from the color field (assuming you use the standard serialization names). See C# MongoDB Distinct Query Syntax for examples of how to do that.

I believe this should work, but have not tested it, assuming that collection is your collection object: var colors = await collection.DistinctAsync<List<string>>("color", "{}").ToListAsync();

Sign up to request clarification or add additional context in comments.

1 Comment

I had never seen the "distinct" method; thanks! let me look into that and I'll report back.

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.