The problem with your first version with positional projection is that it’s ambiguous. Since samples is an array, what if different elements match the two clauses in the query predicate? Which element do you want returned by that projection?
If you don’t intend for the condition to be satisfied by two different elements, you must use $elemMatch in your query. Certainly that’s what the aggregation syntax is doing - after unwind it will only match original elements that match both conditions. Change your original find and it’s going to return only the correct results you want:
Thank you @Asya_Kamsky yes the elemMatch seems to be way faster than the aggregation .I have one thing more to ask.Can i use elemMatch in an pipeline like this:
Hi @Asya_Kamsky.There is something i camt understand.what is the difference between elemmatch projection and elem query.i used find with elemmatch,is this elemmatch query or elemmatch projection i can’t understand