Chapter 1: Basic Aggregation - $match and $project Lab - $match

Hi

Please someone help me with this code:

var pipeline = [ { $match: {“imdb.rating”: { $gte : 7} }, {“genres”: { $nin: [ “Crime”, “Horror” ] } } } ]

It doesn´t work

Could you please be more specific as to which problem you are having?

Appears:

2020-03-26T11:58:33.719-0500 E QUERY [js] SyntaxError: illegal character @(shell):1:28

My recommendation is to define each stage as a variable.

This has 2 benefits.

  1. It pinpoints more precisely syntax error like the one you shown. The error is in one or the other so you could see which one is the problem.
  2. It helps fine tune the pipeline since you can add and remove stage easily from the pipeline. And you can easily modify a stage.

Doing that, I defined the first variable as var match = { $match: {“imdb.rating”: { $gte : 7} } and then shell presented me with 3 little dots. That means the statement is not complete. I see right away that there is a missing closing brace.

This is the original code that doesnt work:

var pipeline = [ { $match: {“imdb.rating”: { $gte : 7} }, {“genres”: { $nin: [ “Crime”, “Horror” ] } } } ]

But if i divide the code, for example:

var pipeline = [ { $match: {“imdb.rating”: { $gte : 7} } } ]

It works

var pipeline = [ { $match: {“genres”: { $nin: [ “Crime”, “Horror” ] } } } ]

It works too.

I don´t know if i can´t join two criteries

I already found the answer, i had to put $match: by each criteria.

I do not understand

You were either missing a brace after { $gte:7}} because the second brace was closing the content of the $match field.
Or you add an extra one because you want the { genres … } inside the same match. Which now i understand is what you wanted.

To make it both inside the same $match it has to be

{
    $match : 
    {
        "imdb.rating" : { ... }  ,
        "genres" : { ... }
    }
} 

Hi All,

I am stuck in this lab. I have read all the above discussions and solutions but i am not getting the answer…

am inside the aggregation database and using movies db… Below is my query in shell

var pipeline = [{$match:{“imdb.rating”:{$gte:7}},{genres:{$nin:{“Crime”,“Horror”}}},{rated:{$all:{“PG”,“G”}}},{languages:{$eleMatch:{“English”,“Japanese”}}}}]

am getting below error…

2020-04-20T13:57:46.234+0530 E QUERY [js] uncaught exception: SyntaxError: expected property name, got ‘{’ :
@(shell):1:49

Can anyone help what am missing.

line 1 character 49 got { and was expecting something else

1 Like

Hi @Balamurali_27145,

Please re-check your aggregation pipeline for the following conditions:

I would recommend you to go through the documentation and then check for these conditions and try to recreate the pipeline.

Please feel free to reach out if you have any questions.

Thanks,
Sonali

No need to quote genres