Chapter 1 Homework Lab match


I am doing this exercise, but my result when execute


is 21

My pipeline is correct?

var pipeline = [ REMOVED ]


Please, do not post potential answers in the forums.

I would suggest to take a look at the $all and $nin operators, they are very helpful for this homework.

José Carlos



I so sorry for paste de result.

I see this operators.

No problem. Hope you find those operators helpful to complete the homework.

José Carlos

I am not able to get the expected result of 23 matching docs. The pipeline I have is giving me 24.

The problem I have is that I keep getting a single document for which “Crime” is in the genres array. I have tried the following ways of trying to exclude the unwanted genres:

$and: [ { field: { $nin: [ "string1" ] } }, { field: { $nin: [ "string2"] } } ]
$and: [ { field: { $ne: "string1" } }, { field: { $ne: "string2"} } ]
field: { $nin: [ "string1", "string2" ] }

But these all give me back the 23 docs meeting the requirements plus a doc containing one of undesired genres".

Anyone able to help or advise what I am doing wrong here?


I would go this way: field: {$nin: ["string1", "string2"]}

I believe the problem might be your languages filter, I would recommend the $all operator. Please give it a try and let us know if you solved the issue.

José Carlos

Hi José,

I am already using $all for language. The languages filter is fine and as expected all 24 docs contain both English and Japanese.

The doc that I should not be getting contains Crime in the genres array. Based on everything I have looked at after an extensive search online, I should not be getting it! Totally stumped.

The document I am getting back that should not be being returned is below (I used $project to show only fields of interest).

    "title" : "Mr. Holmes",
    "languages" : [ "English", "Japanese", "French" ],
    "imdb" : {
        "rating" : 7.4,
        "votes" : 8033,
        "id" : 3168230
    "rated" : "PG",
    "genres" : [ "Crime", "Drama", "Mystery" ]

Thanks for your reply. Any other suggestions very welcome!


Mark McPherson

I had the same issue for a long time. Kept getting 24 documents and it was that Crime one.
All I can say is carlos recommendation of
“I would go this way: field: {$nin: [“string1”, “string2”]}”

should Eliminate your issue if its the Genres.
Without seeing you genres match does just doing matches using your Genres vs the one above give same result?


Thanks for the replies guys. I feel very stupid now, after realising I was using “genre” rather than “genres” as the field name :blush:

Still, very happy to finally get past this lab :grinning::grin::grinning::smiley::smile:

I think I got the match query working correctly. I verified it by converting the match query to a find and got 23 documents back. I am projecting the title alone. But on command db.movies.aggregate(pipeline).itcount(), I get an error

2018-09-24T22:11:22.060-0400 E QUERY [js] Error: command failed: {
“operationTime” : Timestamp(1537841474, 1),
“ok” : 0,
“errmsg” : “A pipeline stage specification object must contain exactly one field.”,
“code” : 40323,
“codeName” : “Location40323”,
“$clusterTime” : {
“clusterTime” : Timestamp(1537841474, 1),
“signature” : {
“hash” : BinData(0,“4atDbmjMrsfT22KO72o3e0kkcJI=”),
“keyId” : NumberLong(“6559514413705986049”)
} : aggregate failed :

Not sure what I need to do or what this error means.

Hi @vaithili,

Please send me your pipeline as a private message.

José Carlos

Hi José,

That’s interesting. “genres” being an array, I was trying to use an array query operator, i.e. $elemMatch. I tried different combination of $elemMatch with $ne or $nin, but this doesn’t work.
Any reason why?

I found a working with $elemMatch but it’s quite convoluted, mixing $not and $eq …

The simple solution field: {$nin: [“string1”, “string2”]} does work indeed.

It’s not the first time I’ve noticed there could be several solution for the same result. Is there a way to trace what’s going behind the scene in Mongo in order to understand which is the best, most efficient solution?




You can take a look at the different stages in the explain output.

José Carlos