Lab1 $match m121

var pipeline = [
{
$match: {
‘imdb.rating’: { $gte: 7 },
‘genres’: { $nin: [‘Crime’, ‘Horror’] },
‘rated’: { $in: [‘PG’, ‘G’] },
$and: [{ ‘languages’: ‘English’ }, { ‘languages’: ‘Japanese’ }]
}
}
]

  • db.movies.aggregate(pipeline).itcount() returns zero.
    I’ve also used { $match:{ “$and” : [
    {“imdb.rating”:{$gte:7}},
    {“genres” :{$nin:[“Crime”,“Horror”]}},
    {“rated” :{$in:[“PG”,“G”]}},
    {“languages” :{$all:[“English”,“Japanese”]}}
    ]
    }
    }
    whats wrong in my pipeline?

There is an implicit $and inside $match. So the second form has unnecessary complexity.

For the languages field the $all form is the way to go.

You can pinpoint which is too restrictive, if any, by running a simplified pipeline where one field or more field is remove. For example, you may start with a pipeline where only imdb.rating is used and see if you have any documents.

But it is entirely possible that you are not connect to the appropriate server or that you are not using the appropriate database.

okay I have already tried with the second case also as you mentioned, just curious to know that do we require to add { } if there are mutiple conditions inside $match
i.e $match : { {condition 1},{condition 2},{condition 3} }

No, the general syntax is:

{ 
  $match :
  {
     field_1 : value_1 ,
     field_2 : { $SomeOperator : value_2 } ,
     ...
     field_n : value_n
  }
}

See https://docs.mongodb.com/manual/reference/operator/aggregation/match/ for more details. A stage is a JSON document so it follows the same construction rules.

Can some explain the answer to this?

I think because of new lines, can you try with a single line for your pipeline variable? once it is collected then, just type ‘pipeline’, then you will get the content of the variable.

As already mentioned:

But it is entirely possible that you are not connect to the appropriate server or that you are not using the appropriate database.

Post screenshot of the running the mongo shell command:

db

Yeah, it worked it was because of the multiline thing, THANKS! @Min_Hong
and… yes, also to mention @steevej-1495 I was on the appropriate server. :slight_smile:

Doubtful, yes in bold, look at the error message, the command is perfectly parsed. It is definitely not a multiple line thing. A multiple line thing will have caused a syntax error.

My mention was

I do believe that you were connected on the appropriate server. But I can confirm that you were not using the appropriate database. Look again at the error message. You will see $db : “movies”. It means you were using the database movies while you were supposed to use the aggregations database.

1 Like

I checked the query

{
“imdb.rating”: { $gte: 7 },
genres: { $nin: [‘Crime’, ‘Horror’] },
rated: { $in: [‘PG’, ‘G’] },
$and: [{ ‘languages’: ‘English’ }, { ‘languages’: ‘Japanese’ }]
}
And it returns 23 documents which is correct

Did you load and run your query through the validateLab instructions at the bottom of the lab.

It will take your number and turn it into another number which you can check against the available selections.

validateLab doesn’t check anything except the number of documents, I check the code
oh its not the reply to me? then sorry, never mind haha

@Eugene_Bos

I was replying to lovch, I just didn’t put use the @ :slight_smile: