Chapter1 ,hw1 ,m121


please help to find the error?

can you add exact query here?

Please specify clearly what issue you are facing.

Hi,
I have error message at the beginning of the following part of my query:
{“languages”:{"$eq":{“English”,"$eq":“Japanese”}} is not accepted.
I have changed it to:
{$and":[{“languages”:{"$eq":“English}},{“languages”:{”$eq":“Japanese”}}]}
which is not accepted too.

var pipeline = db.movies.aggregate([{$match:{“imdb.rating”:"$gte": 7,
{“nor”:[{“genres”:“Crime”},{“genres”:“Horror}]},
{“or”:[{“rated”:“PG”},{“rated”:“G”}]},
{“and”:[{“languages”:{”$eq":“English”}},
{“languages”:{"$eq":"Japanese}} ]} }}])

The query is not being accepted.

Hi @farideh_gorji, as mentioned in my previous answer, your aggregation pipeline has an invalid syntax:

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

Syntax is wrong, the $match is missing the colon punctuation, the match aggregation should be

{$match: {query}}

Check the official docs for understanding the aggregation pipeline.

That was my typing error. In my real query The “:” exists.

operators should be used with $
like $nor, $or, $and

also there are many syntax errors like quotes and brackets are not ended properly

Hi Sourabh,
Would you please take a look at my last post.?

The following is wrong

"imdb.rating" : "$gte" : 7

look at the examples, from https://www.mongodb.com/docs/manual/reference/operator/query/gte/

When your are not sure where is your error decompose into simple parts, also known as divide and conquer.

Some common errors

// when forgetting to put fields with dots in quotes
rating = { imdb.rating : "$gte" : 7 }
// you get error at line 1 character 15
SyntaxError: Unexpected token, expected "," (1:15)

// when not enclosing an object in braces, the object in this case is $gte:7
rating = { "imdb.rating" : "$gte" : 7 }
// you get error at line 1 character 34
SyntaxError: Unexpected token, expected "," (1:34)

// missing : between key and value
rating = { "imdb.rating" { "$gte" : 7} }
SyntaxError: Unexpected token (1:25)

// using = sign rather : to separate key from value
rating = { "imdb.rating" : { "$gte" = 7} }
SyntaxError: Unexpected token (1:36)

// another missing :
rating = { "imdb.rating" : { "$gte"  7} }
SyntaxError: Unexpected token (1:37)

For your $nor:[{genres:Crime},{genres:Horror}] look at https://www.mongodb.com/docs/manual/reference/operator/query/nin/

For your $or:[{rated:PG},{rated:G}] look at https://www.mongodb.com/docs/manual/reference/operator/query/in/

For your $and[{languages: … Japanese}] look at https://www.mongodb.com/docs/manual/reference/operator/query/all/

Documentation is your friend, you will see plenty of example and learn the proper syntax.

Thank you very much.
Farideh

1 Like

In continuation on my divide and conquer approach, I will never write a big pipeline in one line like that. I would break it down in such a way that logical part of the whole query is a variable. In this case, we have 4 parts, we query imdb.rating, genres, rated and languages. It would look like:

// the ... below are the values or operations to match
imdb = { "imdb.rating" : ... }
genres = { "genres" : ... }
rated = { "rated" : ... }
languages = { "languages" : ... }

// then I put them together as a stage (the ... below are object destructuring)
match = { "$match" : { ...imdb , ...genres, ...rated, ...languages } }

// the I put together the pipeline (but here we only have 1 stage)
pipeline = [ match ]

// but if I had many it would look like
// pipeline = [ match , project , limit , sort ]
// where each is a variable

The main advantages is that each line has a reduced number of quotes, braces and brackets. Easier to edit because if you make a mistake, you only edit the part that has the error.

Please read the lab instructions, you would see

Assign the aggregation to a variable named pipeline, like:

var pipeline = [ { $match: { ... } } ]

Why do you do?

var pipeline = db.movies.aggregate( ... )

Please read the answer we post, you will see

I specially mentioned “the … below are object destructuring”.

Why do you do

var imdb = { OperationExpression }
var genres = // details left out
// You did not used the ... of object destructuring
match = { "$match" : { imdb , genres, rated, languages } }

While this works for genres, rated and languages, it fails with imdb because the field name is imdb.rating. See the difference

// The way I shown you
imdb = { "imdb.rating" : { "$gte" : 7 } }
match = { "$match" : { ...imdb } }
// Gives
match === { '$match': { 'imdb.rating': { '$gte': 7 } } }

// Your way 
imdb = { "$gte" : 7 }
match = { "$match" : { ...imdb } }
// Gives the wrong field name
match === { '$match': { 'imdb': { '$gte': 7 } } }

I also mentioned

Thanks for all your efforts trying to help me.
I like to see my own solution is working. It gives me more confidence. That’s why I stick to my $or and $nor notations. But if they are wrong, then I have to change them.

Hi ,
It worked with your Divide and conquer approach and my $nor and $or.
Thanks a lot
Farideh


Is there any reason why when you get error at 1:34 (line 1 character 34) (this position is also indicated with the little red caret) you do not correct the error at line 1 character 34 but you introduce a new one by removing the opening and closing curly braces?