Does error message aid in query crafting?

when one gets these errors - - does the message help point to the error at all…or should one just take it as a generic ‘something is wrong’:

QUERY [js] uncaught exception: SyntaxError: missing } after property list :
QUERY [js] uncaught exception: SyntaxError: missing : after property id :

in this case ‘property’ is… and ‘id’ versus ‘list’ is…

PS: the property id happened to me in properties like tomato.rating, where you have to enclose the property in " " if they are part of a tree.
But sometimes its just a matter of deleting some spaces as I said below… :frowning:

Being new to these queries and this format they are not helpful at all.
I’m having a lot of trouble with my queries because I tend to spread it over several lines and indent the parts for clarity but that is causing errors that disappear as soon as I delete the spaces and put it all in one line.

Does anyone know if there is an ide that can help with creating these queries and keeping track of the {} pairs?

@James_58604,

An error is not a warning. Warnings can be ignored but errors must be dealt with or handled. The line and column numbers are sometimes good indicators, however, there are some cases where MongoDB compile errors aren’t explicit enough/not very helpful.

Using @apachai_54593’s example, here’s a quick breakdown:

@apachai_54593,
A general rule of thumb is that referencing nested fields or sub-documents must be enclosed in quotes. I think this was mentioned in one of the lectures. Same goes for fields containing spaces.

With regards IDEs, you can use MongoDB Compass which provides some Intellisense features like pairing up quotes, field name suggestions (with quotes included) like the screenshot below, etc:
image

For a much richer experience, there’s Studio 3T, however, I prefer the interface of Compass for writing aggregation pipelines. Studio 3T offers more functionality (beyond what MongoDB Compass offers) but I personally don’t like its interface for writing aggregation pipelines.

for those of us that anticipate crafting queries a lot - all of this is helpful…

started w Compass and like it…don’t think we have the connection string to use here - do we? … don’t remember the query field being large enough for a complex nested but perhaps there is an expansion feature I never got to…

I will need to check out the commercial IDE - am soft in terms of accustomed to a visual design…tough to do this in text alone over in notepad and then copy/paste into a command line…

apachai I haven’t run into any space/indent issues - I don’t think…but definitely the pairing of brackets and figuring out if quotes are needed is a pain…

one thing I’m really missing is doing trial runs and seeing query output… that is so helpful - especially in field transforms/calculated fields and such…

thought I saw out in the commercial market a SQL to noSQL translation tool - - that would be neat for people coming over from the SQL side though there clearly are such major schema differences there must limitations to such a tool…

James , there was no connection string, but its possible to connect using the fill options manually.
Just have to fill the host, replica set, user and password.

host : cluster0-shard-00-00-jxeqq.mongodb.net
replicaSet:Cluster0-shard-0
user: m121
password: aggregations

that did work apachai - thank you - didn’t realize it…not sure why this class isn’t teaching us on compass - - my first take of the aggregate UI is that it’s a little confusing…

Because all the labs are full pipelines with multiple stages in one code block, plus the mongo shell has more functionality than Compass. In Compass the UI is built so that you write pipelines per stage, however, there’s nothing stopping you from writing more than one stage in a given pane.

Those are valid points, I will try to get better at the shell.
But I’m having a hard time hunting down { } mistakes and missing commas while learning document based queries, if I am to be honest.The shell takes me 2 or 3 times more compared to compass, and I dont mind the training wheels in this first moment.

I did a partial lookup in chapter 2 and the shell scrolled out the initial lines even though I limited the result to one, so I couldnt see the result to get a grasp on what was happening.

The UI from compass showed me the structure of the result and smoothed the learning curve for me.

I think a java script editor product like VS Code is going to manage brackets - but it doesn’t have any mongo specific features… am doing a superficial review of Studio 3T - I can see 007’s point on the interface not being a nice as Compass but it sure looks feature rich…it is very tempting for that reason …

As a suggestion, write all your queries in a Text editor like Notepad++ or Sublime and you can copy and paste it into the mongo shell without worrying about breaklines… it can handle it. Meaning, your code doesn’t have to be a one-liner. For example, here’s a code block that you can copy and paste directly into the mongo shell:

db.aggregations.aggregate([
  {
    $project: {
      _id: 0,
      name: 1,
      airlines: 1,
      alliance_airlines: 1,
      total: {
        $reduce: {
          input: {
            $filter: {
              input: "$connections",
              cond: {$in: ["$$this.airline.name","$alliance_airlines.name"]}
            }
          },
          initialValue: {
            connections: 0, 
            uniqueDestinations: 0, 
            arrayOfDestinations: [],
            arrayOfAirlines: []
          },
          in: {
            $cond: {
              if: {$in: ["$$this.dst_airport","$$value.arrayOfDestinations"]},
              then: {
                connections: {$add: ["$$value.connections", 1]},
                uniqueDestinations: "$$value.uniqueDestinations",
                arrayOfDestinations: "$$value.arrayOfDestinations",
                arrayOfAirlines: {
                  $cond: {
                    if: {$in: ["$$this.airline.name","$$value.arrayOfAirlines"]},
                    then: "$$value.arrayOfAirlines",
                    else: {$concatArrays: ["$$value.arrayOfAirlines", {$split: ["$$this.airline.name","¬~¬"]}]}
                  }
                }
              },
              else: {
                connections: {$add: ["$$value.connections", 1]},
                uniqueDestinations: {$add: ["$$value.uniqueDestinations", 1]},
                arrayOfDestinations: {$concatArrays: ["$$value.arrayOfDestinations", {$split: ["$$this.dst_airport","¬~¬"]}]},
                arrayOfAirlines: {
                  $cond: {
                    if: {$in: ["$$this.airline.name","$$value.arrayOfAirlines"]},
                    then: "$$value.arrayOfAirlines",
                    else: {$concatArrays: ["$$value.arrayOfAirlines", {$split: ["$$this.airline.name","¬~¬"]}]}
                  }
                }
              }
            }
          }
        }
      }
      ,connections: 1
    }
  }
]).pretty()

It’s multi-lined with multiple open and closes brackets/braces and it has no syntax errors.
PS: It will return 0 results because it’s part of a bigger pipeline.

Or use Compass to write your pipelines stage-by-stage, then get a copy of the entire pipeline using the Export button:
image

I do want to point out to anyone that might peruse this dialog that the first post above by 007 where the code is highlighted - - - that the line and column values can be handy sometimes…

i.e. I crafted a query that threw this error and those values pointed me directly to a typo (wrong bracket type). Just count down the lines and over to the character. It’s not always this straightforward perhaps but it is worth keeping in mind.