$match Lab - Homework

Help MongoDB pick a movie our next movie night! Based on employee polling, we’ve decided that potential movies must meet the following criteria.

  • imdb.rating is at least 7
  • genres does not contain “Crime” or “Horror”
  • rated is either “PG” or “G”
  • languages contains “English” and “Japanese”

I was able to create the $match query , but my agregation is returning 24 records. I validated each of the 24 records and found that they are meeting the above criteria .

  1. Can somebody confirm if they are able to get the 23 records back
  2. the 24 records were returned on the mongo db instance connected using following connection string : 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 . Is this the mongo instance to be used for the lab work?

I found out the reason . It appears to be due to using $elemMatch operator . My understanding was using $elemMatch helps in being able to apply a predicate against each element of an array document. but in this situation it has resulted in returning inconsistent results.

Can you be more specific about the inconsistencies you have observed? So far I have not seed any, As for this specific lab a lot of people had problem with the genres part of the query.

For e.g. Genres is an array type of document . So in order to ensure we don’t want to include Horror and Crime in our result sets if we do a {genres : {$elemMatch : {$nin:[crime,horror]}}}

It would return 24 . And if you inspect the 24 results they will correctly comply. On the other hand if you directly apply $nin operation on genres by removing $elemMatch operator it returns 23 documents .

Please read carefully:

Hi.

I tried using {genres: {$not: {$all: [‘Crime’, ‘Horror’]}}} and got 24 documents. Using $nin I got 23.

I’m curious, (and I might be missing something here) but shouldn’t both solutions return the same thing?

Can you explain why including $elemMatch adds one additional record?

$all : [Crime, Horror] : would return records that include Crime & Horror . So negation of this means return all the records where Genres is NOT (Crime and Horror).

You would miss items where Genre is either only Crime or only Horror .

Hm, awesome.

Thanks for the explanation. :slight_smile:

1 Like

Nevermind. My understanding of $elemMatch operator was not correct. $all in conjunction with $elemMatch would have been appropriate in the said scenario.

Hi All,
i am using below. but , i am more than 23 records,

db.movies.aggregate(< deleted >)

The field genres is an array.

Let’s assume it holds the values Horror, Crime, Drame

For value Horror, $nin Horror,Crime is false
For value Crime, $nin Horror,Crime is false
For value Drame, $nin Horror,Crime is true
So for an array with Hooror,Crime,Drame, $nin Horror,Crime is true
So this document is selected but it should not because it contains Horror or Crime so $nin is not the right operator

Please follow forum guidelines and do not post code that is potential answer.

Please do not post your question in more than one thread. That makes all of us spending time reading the same problem.

Which post of mine are you referring to ??

None. Sorry if the reply was not clear. I was replying to Sushil.

$nin is correct operator for not selecting both Horror and Crime.


db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )
This query will select all documents in the inventory collection where the qty field value does not equal 5 nor 15 . The selected documents will include those documents that do not contain the qty field.
1 Like