It might a long shot, but here we go. Let's suppose I have the following document definition:
public class Test1 { public ObjectId Id {get;set;} public int NonUniqueId { get; set; } public string Name { get; set; } public decimal Price { get; set; } }
Using IMongoCollection<Test1>
, I can filter the results to documents the ones where Name = "somevalue"
, sort by Price
and return 10 rows only.
var builder = Builders.Filters; var filter = builder.Where(x=>x.Name == "somevalue"); var result = await collection.Find(filter).SortBy(x=>x.Price).Limit(10).ToListAsync();
Now to the question at hand. If more than one document is returned by the filter (with Name = "somevalue"
) and NonUniqueId
occurs more than once in that set, I would like to exclude duplicates NonUniqueId
and return just the one with the lowest Price
.
This logic is quite easy to implement in the code: fetch 10 results, if any of them are 'duplicates' (i.e. NonUniqueId
occurs more than once), keep the one with the smallest price and run another search excluding this NonUniqueId
. However, it requires multiple DB calls which is not great (up to 10 in the worst case).
In MongoDB (with C# driver), is there a way to ignore these 'duplicate' results based on the property and, ideally, return just the min of another property (or anything to the same effect)?