M121 - ch 1- $match lab

during the first part of the lab it says the Aggregate should return 23 documents - i am only getting 18 - i wonder if its something i am missing or that maybe i didn’t start with all of the documents. Does the movies collection have 23530 documents? I 've tried different combinations but never get to 18

“$and”:[
{“imdb.rating” : { “$gte”: 7}}
, {“genres” : {"$nin" : [“Crime”, “Horror”]}}
, {“languages” : {"$eq" : “English”}}
, {“languages” : {"$eq" : “Japanese” }}
,{ “rated” : {$in : [“PG”, “G”]}}
]

}

Check this thread
Document count is correct

for languages maybe don’t do $eq , try $in because a movie can have more than one language. We want to make sure english is contained in the languges AND japanese is contained in the languages. I hope this will help

The operator $eq as it is used above is fine as it is smart enough to work on array elements.

mongosh> db.stories.find()
< { _id: 1, entries: [ 2, 3, 4 ] }
mongosh> db.stories.aggregate( { "$match" : { "entries" : { "$eq" : 3 }} } )
< { _id: 1, entries: [ 2, 3, 4 ] }
mongosh>

However, the following would not find any document.

mongosh> db.stories.aggregate( { "$match" : { "entries" : { "$eq" : [ 3 ] }} } )
mongosh>

Furthermore, $in is not appropriate for the lab’s requirement:

  • languages contains “English” and “Japanese”

The $in is equivalent to English or Japanese:

mongosh> db.stories.find()
< { _id: 1, entries: [ 2, 3, 4 ] }
  { _id: 2, entries: [ 2, 3 ] }
mongosh> db.stories.aggregate( { "$match" : { "entries" : { "$in" : [ 3 , 4 ] }} } )
< { _id: 1, entries: [ 2, 3, 4 ] }
  { _id: 2, entries: [ 2, 3 ] }
mongosh> 

Actually, the $and with the 2 $eq is correct despite not being the most concise.

mongosh> db.stories.find()
< { _id: 1, entries: [ 2, 3, 4 ] }
  { _id: 2, entries: [ 2, 3 ] }
mongosh> db.stories.aggregate( { "$match" : { "$and" : [ { "entries" : { $eq : 3 } } , { "entries" : { "$eq" : 4 } } ] }}  )
> { _id: 1, entries: [ 2, 3, 4 ] }
mongosh> 
1 Like