Lab - $match Homework

I am just starting this course and I bookmarked the manuals. I can connect and show a movie document. My issue is with the syntax of the $match. When I am in the terminal and entering my filtering criteria, I cannot get the command to execute when I close it with }]). When I hit enter, the cursor moves to a new line and the command doesn’t try to execute.

Is there any docs other than what is provided that can give me some idea on

  1. How to chain together the filters and
  2. how to get the aggregate command to execute?

Thanks. I searched the forum and I didn’t find any questions with the same direction as what I need.

Please post a screenshot of what you are doing that illustrate the issues your are having.

Well… In all actuality I am having several issues… I am confused as to how best to use the aggragate command. Is the proper syntax for using it in the Homework for chapter one db.movies.aggregate?

I am running it in the terminal (Primary)… Is this where this command is supposed to be run ( I am assuming it is because this is where I run the db.movies.findOne() command.

My confusion is in the Syntax. According to lesson 0, the proper syntax is

db.movies.aggregate ([{
$match: {
key 1: {condition 1}’
key 2: {condition 2},
key N: {condition N}
}
}
])

Where I get confused is if I am writing a match for a field in a array, do I use
db.movies.aggregate([{
$match: {array: [$in: {value 1, value2}]
}])

Would this effectively querry an array? According to the video in chapter 0 on syntax, it shows the following:

db.movies.aggregate([{
$match: {
Espression1: {conditon}
Espression2: {condition}
}])

Also, my issue is when I finish and put the }]) on the end, after I press enter, the command does not execute. Do I need a ; to end the command. I will post what I am typing in the terminal soon but I needed to get this answered first.

A pipeline is simply an array of JSON documents, where each document is a stage.

JSON field names with dots in their name, like imdb.rating, must be in quotes.

The general format of a document is { Field_Name_1 : Value_1 , Field_Name_2 : Value_2 , … , Field_Name_n : Value_n }

The string { “7” } does not follow the correct syntax.

Sorry. Was away from my computer for a couple days. Thanks for your help. I do have one more however. If I am working with an array is the {Fields_Name: Value} syntax like this {genres: [{$gte: {7}}]} for that one specific match query?

No it is not. The syntax { 7 } is as wrong as { “7” } is since there is no field name.

You will find the full syntax at:

The M001 course covers some of the syntax.

Hi David,

read your post and most of the replies here. It seems you’re having issues with the right syntax for your aggregation pipelines.

Did the M121 couple weeks ago, and as I started, me, too, I certainly had issues with my syntax then.
So I decided, I’m gonna need a syntax checker to manage all that opening and closing braces and brackets.

From the get go, I had not so much luck working directly in MongoDB Compass or in the Mongo Shell.

However, I “discovered” that the Mongo Shell did accept writing my documents in JavaScript like db.users.find({ name: 'John Doe' }) , it was not necessary to write my documents in JSON like db.users.find({ "name": "John Doe" }) .

Therefore, I could just grab my Vim or Emacs and write my dcuments – and later aggregation pipelines – inside a JavaScript file enjoying all the syntax highlighting and checking that were to be enjoyed.

First writing my documents using Vim / Emacs / VSCode JavaScript editor and then copying the ready-made syntax in my Mongo Shell was kind of the breakthrough.

By the way, I soon discovered the MongoDB Extension for VSCode with its Mongo Playgrounds. Brought it all one notch higher …

Maybe such would help you, too? Regards, M.


Update #1 at Thu Feb 25 16:39:01 CET 2021

Please also consider the reply from @steevej-1495 below, which is true and might very much prevent beginners from being mislead by my simple code eamples demonstrating some degree of acceptance of JavaScript code within the Mongo Shell.

@steevej-1495 , thank you very much for your commitment and for commenting my short remarks.

Good recommendations from @Uwe_Scheffer. But 2 things:

  1. The quotes on @David_44057 post are wrong because the code snippet is not properly enclosed between triple back ticks, <code> or <pre> tags.
  2. Beware than when the a field name contains dots like imdb.rating quotes, single or double, are still required. So I took the habit to always use them. And I prefer the double quotes as it is consistent with other programming languages I use.