What is incorrect in this query to find the no of records with a particular value

No, you are not, but I was.

{ "reviews" : object } means strict equality. So the query will only match documents that have the field reviews exactly equals to the object, all the same fields and all the same values in the same order.

Ooops

saru mo ki kara ochiru

So you were right and the query {address:{city:NY}} was wrong because address has other fields.

// starting with
[
  { _id: 0, a: { b: 1 } },
  { _id: 1, a: { b: 1, c: 2 } }
]

mongosh> c.find( { a : { b:1 } } )
[ { _id: 0, a: { b: 1 } } ]


mongosh> c.find( { a : { b:1 , c:2} } )
[ { _id: 1, a: { b: 1, c: 2 } } ]

mongosh> c.find( { a : { c:2 , b:1} } )
// no result because object {c:2,b:1} is not equals to object {b:1,c:2} just like direct JS

mongosh> c.find( { "a.c" : 2 , "a.b" : 1 } )
[ { _id: 1, a: { b: 1, c: 2 } } ]

And I actually knew that: Any drawbacks in having _id an object - #5 by steevej

Ditto with arrays Problem matching values in array - #4 by steevej

So I was asleep, now I am awake, … I think.

1 Like