I have a document in this format:
{
_id: ...,
myArray: [{other: stuff}, {other: stuff}, ...],
...
}
I want to find elements that match certain things, like the _id or fields value from the sub-documents in myArray.
I want to return the documents, but with a filtered MyArray where only the matching sub-documents are present.
I tried to do a projection and include the matched elements like this:
_mongoContext.myDocument
.Find(x => x.id == id & x.myArray.Any(y => myList.Contains(t.other)))
.Project<myModel>(Builders<myModel>.Projection.Include("myArray.$"))
This, I think, should only return the first element that matched in myArray instead of all documents, which is not what I want (I want all sub-documents that match the query to be present in the returned document).
And anyway it did not even work, I'm getting a positional projection does not match the query document error. Maybe it's because I'm not using FindOne?
In any case, how can I achieve what I'm looking for? (See question in bold)