[SOLVED] Chapter 1 Optional Lab Pi Day

I am successfully finding and adding a field called labor of love:

{ "title" : "The Sign of the Cross", "cast" : [ "Georges M�li�s" ], "directors" : [ "Georges M�li�s" ], 
"writers" : [ "Georges M�li�s" ], "labor of love" : true }

When I try to match on that field by a second match at the end of the pipeline I get an incomprehensible error message

matchStage2 = { $match: { 'labor of love': true } }

The above match stage fails when I add it as the final stage in my pipeline.

matchStage2 = { $match: { 'title': 'The Sign of the Cross' } }

The above matchStage2 works just fine and outputs the document above.


Here is the error message
2019-03-14T14:21:54.556-0400 E QUERY [js] Error: command failed: {
“operationTime” : Timestamp(1552587708, 1),
“ok” : 0,
“errmsg” : “$in requires an array as a second argument, found: missing”,
“code” : 40081,
“codeName” : “Location40081”,
“$clusterTime” : {
“clusterTime” : Timestamp(1552587708, 1),
“signature” : {
“hash” : BinData(0,“arZu+PycMAWrg/vDnAqfnoldKjQ=”),
“keyId” : NumberLong(“6627672121604571137”)
} : aggregate failed :

I found my issue.

I failed to check if all three of writers, cast, directors exist. When I check for that, my match stage works as expected.

The optimizer (grrrr I wish I could override this behavior) was moving part of my last matchstage up so that is why it worked.

In general, I find debugging aggregation pipelines very difficult. A javascript routine would be so much easier to debug.


@ robkat

Glad you found your problem; I am marking your post as “SOLVED” so other students know that this has been solved here if they’re having a similar problem.

You may find using Compass to generate aggregation statements is easier for you to debug BTW. Also, if you use Compass, you will see that it parses each element of the pipeline for correctness before moving on – that’s generally a good strategy from the command line as well. HTH.