I have following documents in product collection.
db.product.find()
{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 }, { "product" : "xyz", "score" : 5 } ] }
{ "_id" : 2, "results" : [ { "product" : "abc", "score" : 8 }, { "product" : "xyz", "score" : 7 } ] }
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }
The following query returns the result as expected with elemMatch.
> db.product.find( { results : { $elemMatch : { product : "xyz" , score : { $eq : 5} } } } )
{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 }, { "product" : "xyz", "score" : 5 } ] }
Similarly this also returns the expected results.
> db.product.find( { results : { product : "xyz" , score : 5 } } )
{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 }, { "product" : "xyz", "score" : 5 } ] }
But when I use comparison operator inside an array, I am not getting any results.
db.product.find( { results : { product : "xyz" , score : { $eq : 5} } } )
I am unable to figure out this unexpected behavior.