Error Identification

This is my query and the error I am getting please tell me where I am wrong. This is the first assignment in M121.
var pipeline = [< deleted >]
2019-03-18T00:56:07.660+0530 E QUERY [js] SyntaxError: invalid property id @(shell):1:53

Please do not post potential answers.

The error message tells you that you have an invalid property id. Split your pipeline in simpler parts until you pinpoint the error.

We’re not supposed to post our code snippets for the labs here, so I suspect a moderator will remove that part of your post soon, but I can see your problem. The most important part of the error message is this:

SyntaxError

This means that the Mongo shell couldn’t understand your command, so didn’t even try to execute it. In my (limited) experience, this is often caused by the opening and closing curly brackets or square brackets not matching up correctly, or a missing comma between JSON fields, or a missing colon after a JSON key.

The next most important part of the error message is the location of the error:

@(shell):1:53

This means the error was in the command you typed into the shell, at line 1 (of 1), character position 53.

I’m not going to give you the answer, but I offer you these suggestions…

Rather than trying to type a complex expression into the Mongo shell, create a .js file in the directory that you run the Mongo shell from (I’ve called it “myQuery.js” but you can call it whatever you want).

Edit this .js file using your favourite text editor which is aware of JavaScript. ECMAScript / JSON syntax (e.g. Notepad++, Visual Studio, Atom, others are available).

Spread your command out over more than one line, starting a new line and indenting each time you encounter an open curly or square bracket, and unindenting when you close that bracket, e.g.

var pipeline = [
  {
    $match: {
      <first match condition>,
      <second match contition>
    }
  }
];

Run this .js file from the Mongo shell:

load("myQuery.js")

The error message will now tell you which line within your .js file the syntax error occurred at, which makes debugging easier.

And have a look at this post regarding using JavaScript / ECMAScript variables to split a complex expression into smaller, more understandable and more manageable chunks: Aggregate() arguments - breaking the command down into variables (this works for me anyway).

Good luck :+1:

2 Likes

@Simon_39939

Excellent advice. Let’s hope the student takes it and solves this. Thanks.

@Tasheer_75682

Please follow Forum guidelines and do not post code here. I have edited your post to remove that. And both @steevej-1495 and @Simon_39939 have some good advice below. Good luck.