In C# using the .NET MongoDB driver, I am trying to retrieve objects that are in a nested array of a document and that match some criteria.
Models:
public class MainElement
{
[BsonId]
public ObjectId DocumentId { get; set; }
public string A { get; set; }
public string B { get; set; }
public List<NestedElement> NestedObjects { get; set; }
}
public class NestedElement
{
public DateTime Date { get; set; }
public string W { get; set; }
public long Y { get; set; }
}
What I want to retrieve is the property A from MainElement and properties Date, W, Y from NestedElement for all documents thats matches my filters.
So far I tried this but it returns the full MainElement document:
var filters = Builders<MainElement>.Filter.Eq("A", "Something") &
Builders<MainElement>.Filter.Eq("B", "AnotherThing") &
Builders<MainElement>.Filter.Gte("NestedObjects.Date", day) &
Builders<MainElement>.Filter.Eq("NestedObjects.W", "ABC") &
Builders<MainElement>.Filter.Gte("NestedObjects.Y", 1500);
var projection = Builders<MainElement>.Projection.Include("A")
.Include("NestedObjects.Date")
.Include("NestedObjects.W")
.Include("NestedObjects.Y");
var elements = Collection.Find(filters).Project(projection).ToList();
Any ideas?
day, any NO's W property (not necessarily the same NO as the previous requirement) is equal to ABC, and any NO's Y property is equal to 1500, as opposed to only documents where all of those are true for a single nested object?