Lab1: $match Lab is not clear enough


After I create the variable pipeline, please correct me: — I will delete as soon as I understand my mistake—

var pipeline = [ < deleted > ]

I got no error and nothing happens so I assume it is right, then I do:


And I get:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.aggregate(pipeline).itcount()
2019-07-17T19:09:11.204+0300 E QUERY [js] Error: command failed: {
“operationTime” : Timestamp(1563379742, 6),
“ok” : 0,
“errmsg” : “A pipeline stage specification object must contain exactly one field.”,
“code” : 40323,
“codeName” : “Location40323”,
“$clusterTime” : {
“clusterTime” : Timestamp(1563379742, 6),
“signature” : {
“hash” : BinData(0,“OUQIDapI0OaFkC/be2yBf1vzJOk=”),
“keyId” : NumberLong(“6661714504839069697”)
} : aggregate failed :

1 Like

Hey @Ridah_97796

The syntax on your pipeline is incorrect. Must be in the form

db.collection.aggregate( [ { <stage> }, ... ] )

If you are trying to Match a document with all those fields it would be more like

var pipeline = [
  $match: { 
      “imdb.rating”: {$gte:7},
      “genres”: ...,
      "rated": ....,

I think also you will have to look more closely at the queries themselves. Best of luck!



Please follow Forum guidelines and do not post code or answers or potential answers in the Forum. I have edited your post to remove that. And it’s nice that you offer to remove it AFTER it’s been published, but really that’s not acceptable. Please do not do this again. Thanks.

Also the fact that your pipeline does not throw an error does not mean it’s correct – merely that it isn’t immediately a syntax error. As you can see from the error you do get, the error only shows up when you attempt to execute the pipeline. I would suggest that you debug this by executing a single stage in the pipeline at a time and see what happens.

I apologize for that.

Actually the error happens when I do var pipeline.
Exactly here in x position:

var pipeline = [
$match: {x “imdb.rating”: ,…

The shell always tells me there something wrong here. I tried to put all queries between [ ] and didn’t work either.

1 Like


OK, no problem. So that gives us a place to start. I’m going to assume that the following are true: you are in the supplied Atlas cluster, you are in the ‘aggregations’ database, and you can run and have reviewed the output from ‘db.movies.findOne()’. (Based on your question, all those are true I think – if not, let me know).

So start from first steps. Check that the command

db.movies.aggregate( [ { $match: { "year": 1913 } } ]).count()

runs correctly – it should return the value ‘13’.

Assuming that works, then enter

var pipeline = [ { $match: { "year": 1913 } } ]

This should not return an error. If it loads correctly , then run


which should show the full list of results. If any of those don’t work, post the result back here and we’ll try to help.

Notice that these are NOT part of the Lab question, so posting actual code as shown will be fine. :wink:


Okay I will try to use the same approach. Thanks.

Here is the first command result:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.aggregate( [ { $match: { “year”: 1913 } } ]).count()
2019-07-19T16:37:29.106+0300 E QUERY [js] TypeError: db.movies.aggregate(…).count is not a function :

you have to use itcount() instead of count()

Yes, .itcount() worked and the result is 13.
I run

var pipeline = [ { $match: { “year”: 1913 } } ]


They worked and showed me the full list of results.

Now when I use “imdb.rating” normally it works alone. When I try to use the followings alone each they don’t work:

  • genres does not contain “Crime” or “Horror”
  • rated is either “PG” or “G”
  • languages contains “English” and “Japanese”


Sorry – my error; I should have indicated ‘find’ in the first command rather than ‘aggregate’. The way to test each of your queries is to put them into a ‘find’ command to be sure that they’re correct. For example, to find all documents in ‘movies’ that are rated either “PG” or “G”, you could use

db.movies.find( { "rated": { $in: ["PG", "G" ] } } 

and so on. Once you have the working queries, you should be able to easily put those into the pipeline $match stage or stages to get your results.

1 Like

The problem with me is turned out very surprising !!

The first command doesn’t work, and the second works. Can you see the difference?

The difference only in the quotation shape. Because I write my code in SublimeText then I copy paste it. So that ruins the code !!


Hey I have seen the quote problem on here a few times. Just got to pay attention I suppose when copying code from a markdown quote. :slight_smile:

As for your error, my suggestion would be to launch the mongo shell in the same directory as the file. See it that works.



This is an important thing to observe. Notice that the mongo shell does not recognize any type of “fancy” quotes; if you use a text editor that is designed for text documents, it may very well automatically substitute appropriate left or right quotes for the basic glyph. I would suggest using an IDE text editor, or a very, very simple text editor to ensure that the quotes are just quotes. :wink:


how do you load the validateLab1.js into the mongo shell?

You need to download the file form “Download Handouts” in lab’s right side of the screen. Wherever directory you save this file, run Mongo shell from there.

1 Like