M121: Match a field with $and operator

Why filters {$and:[{awards:/Won/},{awards:/Oscar/}]} and {awards:/Won/, awards:/Oscar/} are different?

My three steps scenario:

  1. mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl -u m121 -p aggregations --norc
  2. db.movies.find({awards:/Won/, awards:/Oscar/}).count() got answer 2670
  3. db.movies.find({$and:[{awards:/Won/},{awards:/Oscar/}]}).count() got answer 914

A query is a json document. By definition a json document cannot have 2 different values for the same field. The query

is wrong. It is equivalent to

{ awards : /Oscar/ }

You may try the following in the mongo shell.

MongoDB > query = { awards : "Won" , awards : "Oscar" }
{ "awards" : "Oscar" }
MongoDB > query
{ "awards" : "Oscar" }
MongoDB > query = {"$and":[{"awards":"Won"},{"awards":"Oscar"}]}
{ "$and" : [ { "awards" : "Won" }, { "awards" : "Oscar" } ] }
MongoDB > query
{ "$and" : [ { "awards" : "Won" }, { "awards" : "Oscar" } ] }
1 Like