M121 Lab 1 $match huh

I am really stuck on one aspect of the lab. I am using VS Code running the playground and I can get the imdb.rating and rated portion. It narrows the search down to 1070 records. (I hope I am right…)

I removed the code after reading about not posting potential answers.

running this against the movies collection in the aggregations database produces the following printout in VS Code.

[
  {
    "Total Records": 1070
  }
]

So one would assume that I am on the right track. I now just need to get the arrays queried to further filter the movies database but I cannot get the $in operator to work. I have looked at the mongo documentation but there is no specific information on how to include the $in: operator into the $match portion of the query.

I am considering chaining multiple $and operators together. Would this be going in the right direction? Kind of like a nested and?

This is the $in operator you can use in the $match aggregation stage.

@Prasad_Saya,
Thanks… That helped a lot… I got all the operators to work, however, I come up with 877 documents. Without posting my code, I am wondering if order of operation make any difference. Do I search for the $or first and then filter again based on the $and? I currently have my $or statement nested in the $and statement.

Basically, I’m anding my imdb.rating, languages, and genres together, then executing the $or statement on the field “rated”.

Hello @David_Thompson, you can use all matching criteria within a filter - that is in one $match stage. It can be combination of or’s, and’s, … The nesting of the conditions depends upon what you are trying to filter. You need to tell what you are trying to filter…

… in plain English language.

There are examples of using these together at:

Sorry if I am confusing anyone…

Currently I am grouping my fields imdb.rating. languages, and genres in the and and nesting an or statement for the rating right after the last condition.

$and[{
“field”: condition,
“field”: condition,
“field”: condition
$or[{“field”: condition, “field”: condition}]
}]
This is what gets me to 877… I’m pretty sure it is in either the $and or the $or statement… Will read your links. Thanks

Hello @David_Thompson,

If you are trying the condition (fruit == "apples" OR fruit == "oranges"), for this you use the $in, I had linked earlier:

fruit: { $in: [ "apples", "oranges" ] }

If you are trying the condition (fruit == "apples" OR fruit == "oranges") AND (price < 10), then the filter will be:

$and: [
  { fruit: { $in: [ "apples", "oranges" ] } },
  { price: { $lt: 10 } }
]

Hope this is useful!

1 Like

Ok… Read the documentation on $or and $and statements.

To my knowledge, $and can just be combined and it will select documents that ONLY meet the and statement. Therefore, you should be able to search for imbd.rating AND language AND genre.

This should return all documents that meet the criteria for the search. GTE 7, English and Japanese included, and not containing Crime and Horror…

Now to get the rated field sorted, you should be able to nest an $or statement in the filter…
According to documentation, you should use the $in operator instead of an $or statement.

Following all of this, I still come up with 877 documents instead of 23. Is there anyone I can talk to privately so I can share my aggregation filter so I can get some targeted advise on where I am going wrong?

@David_Thompson, since you are having an AND for each of the fields (rating, language, genre, etc.), you should make sure that each of the field’s condition is built correctly (for example, I have shown above there are two fields each matching its own way).

You can try using the match stage for one field / condition at a time, the returned results should be different and with multiple conditions the results should be reducing.

Thanks… I just realized that there is no indication as to where my filter is stopping.

If I read your message right, I should be breaking each condition out into its own $match statement right? Therefore I would have 4 match statements and I can watch the results go down for each statement. Is that what you are referring to?

If I break them out into their own match statement, I remove the $and statement right?

Also, It is my understanding that to get the fields that are arrays that “INCLUDE” the types I use the $in operator. To exclude fields, I use the $nin operator right?

basically to list all record that CONTAIN English and Japanese you would use
$in: [“English”, “Japanese”]

To list all records that do not contain Crime or Horror you would use

$nin: [“Crime”, "Horror]

Am I right?

You can have four match stages - thats okay while trying and figuring how many are getting filtered at each stage. Normally, you just use one match stage with four conditions (as in my example, it uses two conditions put together with an $and operator).

Correct.

The rest you can try and see the results yourself. Look at each field’s condition closely and individually. Then code.

This means the field language is English OR Japanese.

@Prasad_Saya,
I want to thank you soooo much for your help… I found my problem but I now have one last hurdle.

I have 24 documents not 23. I projected them out so I can see the document fields (imdb.rating, languages, genres, and rated) and I have the correct information in my 24 documents.

I double checked my spelling in all fields and conditions, I find no errors. Where do I go from here?

I am using VS Code connected to the cluster and I am running my query in the playground.

Any ideas on what to check next… (I only need to drop one more document. LOL)

@David_Thompson,

I cannot guess what the issue is. It is possible there is a mistake somewhere - where I dont know.

Are you sure you have coded the languages field condition correctly - it should match both languages together (that is the movie should be available in both the languages).

I believe I did… I used the $all: operator. and all entries have both English and Japanese in the array.

I used the project portion of the aggregation and I can see the records with the _id field. All records are unique and none duplicate. So… Do you know who I can talk to offline that would be able to help me with figuring this out? I have double checked my spelling for my fields and conditions.

genres
languages
rated
imdb.rating

I have double checked my conditions

$gte 7
“English”
“Japanese”
“Crime”
“Horror”
“G”
“PG”

Don’t have any idea why I am one record off.

My project statement is:

{$project:{ "_id": 1,"imdb.rating": 1, "languages": 1, "genres": 1, "rated": 1}}

my 24 documents are:

[
  {
    "_id": {
      "$oid": "573a1395f29313caabce2999"
    },
    "languages": [
      "English",
      "French",
      "German",
      "Italian",
      "Japanese"
    ],
    "imdb": {
      "rating": 7
    },
    "rated": "G",
    "genres": [
      "Adventure",
      "Comedy",
      "Family"
    ]
  },
  {
    "_id": {
      "$oid": "573a1396f29313caabce476b"
    },
    "languages": [
      "English",
      "Spanish",
      "Japanese"
    ],
    "imdb": {
      "rating": 7
    },
    "rated": "PG",
    "genres": [
      "Action",
      "Western"
    ]
  },
  {
    "_id": {
      "$oid": "573a13bdf29313caabd59987"
    },
    "languages": [
      "English",
      "Japanese",
      "Mongolian"
    ],
    "imdb": {
      "rating": 7
    },
    "rated": "PG",
    "genres": [
      "Documentary"
    ]
  },
  {
    "_id": {
      "$oid": "573a1398f29313caabce91f1"
    },
    "languages": [
      "English",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.2
    },
    "rated": "PG",
    "genres": [
      "Action",
      "Drama",
      "Family"
    ]
  },
  {
    "_id": {
      "$oid": "573a139cf29313caabcf5f6e"
    },
    "languages": [
      "Japanese",
      "English"
    ],
    "imdb": {
      "rating": 7.2
    },
    "rated": "PG",
    "genres": [
      "Animation",
      "Action",
      "Adventure"
    ]
  },
  {
    "_id": {
      "$oid": "573a13a9f29313caabd1f18a"
    },
    "languages": [
      "English",
      "Italian",
      "Japanese",
      "Yiddish"
    ],
    "imdb": {
      "rating": 7.2
    },
    "rated": "G",
    "genres": [
      "Animation",
      "Adventure",
      "Comedy"
    ]
  },
  {
    "_id": {
      "$oid": "573a1397f29313caabce6136"
    },
    "languages": [
      "English",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.3
    },
    "rated": "G",
    "genres": [
      "Animation",
      "Adventure",
      "Family"
    ]
  },
  {
    "_id": {
      "$oid": "573a1398f29313caabcea314"
    },
    "languages": [
      "English",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.3
    },
    "rated": "PG",
    "genres": [
      "Animation",
      "Action",
      "Adventure"
    ]
  },
  {
    "_id": {
      "$oid": "573a1399f29313caabcec744"
    },
    "languages": [
      "English",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.3
    },
    "rated": "PG",
    "genres": [
      "Comedy",
      "Drama",
      "Fantasy"
    ]
  },
  {
    "_id": {
      "$oid": "573a13abf29313caabd26495"
    },
    "languages": [
      "Japanese",
      "English"
    ],
    "imdb": {
      "rating": 7.3
    },
    "rated": "G",
    "genres": [
      "Animation",
      "Adventure",
      "Comedy"
    ]
  },
  {
    "_id": {
      "$oid": "573a1396f29313caabce35d1"
    },
    "languages": [
      "English",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.4
    },
    "rated": "G",
    "genres": [
      "Drama",
      "War"
    ]
  },
  {
    "_id": {
      "$oid": "573a1397f29313caabce667c"
    },
    "languages": [
      "English",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.4
    },
    "rated": "PG",
    "genres": [
      "Comedy",
      "Drama",
      "Romance"
    ]
  },
  {
    "_id": {
      "$oid": "573a13e7f29313caabdc8bb0"
    },
    "languages": [
      "English",
      "Japanese",
      "French"
    ],
    "imdb": {
      "rating": 7.4
    },
    "rated": "PG",
    "genres": [
      "Crime",
      "Drama",
      "Mystery"
    ]
  },
  {
    "_id": {
      "$oid": "573a1396f29313caabce4280"
    },
    "languages": [
      "English",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.5
    },
    "rated": "G",
    "genres": [
      "Drama",
      "History",
      "War"
    ]
  },
  {
    "_id": {
      "$oid": "573a1397f29313caabce8b90"
    },
    "languages": [
      "English",
      "Russian",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.6
    },
    "rated": "PG",
    "genres": [
      "Comedy",
      "Drama",
      "Romance"
    ]
  },
  {
    "_id": {
      "$oid": "573a13c7f29313caabd7608d"
    },
    "languages": [
      "Japanese",
      "English"
    ],
    "imdb": {
      "rating": 7.6
    },
    "rated": "PG",
    "genres": [
      "Animation",
      "Adventure",
      "Comedy"
    ]
  },
  {
    "_id": {
      "$oid": "573a13caf29313caabd7d6da"
    },
    "languages": [
      "English",
      "Japanese"
    ],
    "imdb": {
      "rating": 7.7
    },
    "rated": "G",
    "genres": [
      "Animation",
      "Adventure",
      "Family"
    ]
  },
  {
    "_id": {
      "$oid": "573a1398f29313caabcea665"
    },
    "languages": [
      "English",
      "Japanese",
      "Mandarin"
    ],
    "imdb": {
      "rating": 7.8
    },
    "rated": "PG",
    "genres": [
      "Drama",
      "History",
      "War"
    ]
  },
  {
    "_id": {
      "$oid": "573a1399f29313caabcec47d"
    },
    "languages": [
      "Japanese",
      "French",
      "English"
    ],
    "imdb": {
      "rating": 7.8
    },
    "rated": "PG",
    "genres": [
      "Drama",
      "Fantasy"
    ]
  },
  {
    "_id": {
      "$oid": "573a13a6f29313caabd1907d"
    },
    "languages": [
      "Japanese",
      "English"
    ],
    "imdb": {
      "rating": 7.9
    },
    "rated": "PG",
    "genres": [
      "Animation",
      "Drama",
      "Fantasy"
    ]
  }
]

Ideas?

Interesting… If you count the _id fields, there is only 20… Don’t know why VS Code playground shows 24 records… This really interesting… Gonna try running the command in the terminal to see what happens.

@David_Thompson, just one last check - make sure the provided input collection document count is correct (has all documents as provided to you).

According to my connection in VS Code

I connected to the mongo instance with the connection string provided.

The aggregations db
movies collection has 44,488 documents in the collection. But I have no way of ensuring this is the correct number. (nothing to compare it to)

@David_Thompson, I suspect the number of documents count is not correct. Please check your Course Material - where you had created the collection.

M121 Lab 1 Homework does not have you create the collection. You log onto the M121 cluster via the connections string provided at the beginning of class.

mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl -u m121 -p aggregations --norc

I switched to the aggregations database
use aggregations
I ran the db.movies.count()
It returned 44488
This number matches the document number in my VS Code connected to the database.
However, When I run my query, I get 24 documents… I need to show some one my query so it can be double checked for accuracy.

The interesting thing is when I run the
{$count: “Total Records”} at the end of the aggregation, it returns 24 for the count. This is also the number returned when I run the match in the terminal.

However, when I run the command
{$project:{"_id": 1, “imdb.rating”: 1, “languages”: 1, “genres”: 1, “rated”: 1}} the printout only has 20 documents. When I run it in the terminal… 24 print out. However, all fields meet the filter criteria. Would love to show someone my query and see if I am wrong anywhere but the forum states not to post the solution… Is there anyone that I can talk to that I can get a private conversation going to look into where my errors are?

@David_Thompson, In the terminal (mongo shell) the display shows only 20 documents at a time. At the end of the display, if you type "it" (without quotes - you will sees a prompt Type "it" for more) it will display the remaining documents.

Yes… I know… I am referring to the playground in VS Code… there is no ability to continue to itterate through the array in VS code running the aggregate command in the playground. I am trying to find out how I can edit the max number of records to display in VS Code running the MongoDB extension.

@David_Thompson, I don’t use VS Code, so I don’t know how it works. You can try posting another question about using the VS Code as you want.