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.

Thomas
  • 10,933
  • 14
  • 65
  • 136

1 Answers1

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();

Kdawg
  • 1,508
  • 13
  • 19
  • I had never seen the "distinct" method; thanks! let me look into that and I'll report back. – Thomas Jul 20 '17 at 15:08