Not able to complete Homework 1 of chapter 1

I am getting a “You aren’t returning the correct number of documents”

This is the script I am using:

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

MongoDB Enterprise Cluster0-shard-0:PRIMARY> load(‘validateLab1.js’)

true

MongoDB Enterprise Cluster0-shard-0:PRIMARY> validateLab1(pipeline)

You aren’t returning the correct number of documents

Why is it not working? Thanks in advance

Hello @Daniel_Martinez

you may want to checkout the structure of aggregate() it takes an array of stages.

To archive less complexity you can all eye on $nin

And finally: It would be helpful for others to read and respond to your question if you could apply the proper code formatting to your post:

Hope that helps, best of luck for your M121 journey.

Regards,
Michael

Hi Michael, I surrounded the statement by using square brackets. Still getting an error:

var pipeline = [ **{** $match : { $and: [ {"imdb.rating": {$gte : 7}},{genres : {$not: {$all: ["Crime", "Horror"]}}}, {$or:[ {"rated": "PG"}, {"rated": "G"}]},{"languages": {$all: ["English", "Japanese"]}}]}}]

I am getting a 24 document count with the above query

Hey @Daniel_Martinez,

I would recommend you to go through the following operator and then re-build your pipeline using them:

Also check the documentation for the following operators to understand their usage:

Note: For simplicity, you can remove the $and operator at the beginning of the pipeline as the comma-separated query filters are ANDed by default.

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

Kind Regards,
Sonali Mamgain

Hi please see my hint in the initial post :wink:

Update:

Sorry @Sonali_Mamgain, I missed that you already answered

Hello,

I’am trying to complete this exercise aswell but I can’t load the file the exercise requires into the mongo shell.

Please take a look at the picture , it contains the shell , the file path to m121 and the exercise requirements:

You have to start mongo shell from the directory where the file resides. Alternatively, you can specify the full path name of the file.

2 Likes

@steevej Wow man ! yes , yes ! Thanks , you are right !

Hello , I have sucessfully completed this exercise but I have question. about the query :

I was trying to run this command without the $and operator , because I thought that if I separate the content of the match by commas as some of you said, it would consider it an “$and” operation. However that does not seems to be the case since I get a Syntax error If I try to run this without the {$and : [ ] } . Can someone tell me why ?

 
//Removed... Possible answer

By removing the $and and the brackets. you end up with:

$match :
        {
                    {"imdb.rating" : {$gte : 7}}, 
                    {"genres" : {"$nin" : ["Crime", "Horror"]}} , 
                    {$or : [{"rated" : "PG"}, {"rated" : "G"}]}, 
                    {"languages":"English"}, 
                    {"languages" : "Japanese"}

        }

you do end up with an invalid JSON document for the $match object. After the opening brace of $match you need a key. So to make it a valid json you will have to also remove the opening and closing braces of each line. But languages will cause you an issue as you cannot have 2 fields with the same key. Well you can, but only the last occurrence is kept.

1 Like

Hi,
can you tell me what’s wrong in below …I am suspecting $in operator(aggregation)

db.movies.aggregate([{ $match:{ “imdb.rating”:{"$gte":7}, “genres”:{"$nin":[“Crime”, “Horror”]},“rated”:{"$in":[“G”, “PG”]},“languages”:{"$in":[“English”,“Japanese”]}}},{ $group: { _id: null, count: { $sum: 1 } } }])

I got the answer. Thanks

1 Like