Is using double quotes compulsory to enclose property names?

In this video:

The teacher using random syntax. In some places he uses double quotes to enclose even the “$project” & “$match”, in some places even for property names he does not enclose with double quotes…!

What is the correct syntax…! Can someone please explain…!

I am very confused with this mixed syntax what he is using…!

Please read:

1 Like

According to the Mongo documentation, when using an operator you should not put quotes on them, although if you put them, nothing will happen either.

The $project stage has the following prototype form:

{ $project: { <specification(s)> } }

You can see it for yourself at:

I search for the word quote in the 2 links you provided and I could not find it. I could not see where it is written that you should not use quotes. They do not use quotes in the examples, except where they use dot notation.

They do not use quotes because in JS based mongo and mongosh quotes are not mandatory for field names in JSON documents, except when using dot notation. And a query is simply a JSON. An aggregation pipeline stage is a document with the only key being the stage name.

So I stand by my comment in the linked post:

Actually I was just answering Balaji_KR, to his doubt if the operators should be wrapped with quotes.

Ok, now expanding my answer to Balaji_KR a bit further, I’m going to assume that when you refer to property names they are field names. So field names within the mongodb context are always strings. And they do not have quotes with some exceptions.
The mongo shell is an interactive JavaScript interface to MongoDB.

Now I’m going to leave you this link that will help you understand when to use the quotation marks

1 Like

And the last sentence of the Stack Overflow post is:

So, I find it easier to be in the habit of using quotes so you don’t have to remember the exceptions.

Which is along the way

Yes it is. I just wanted to help him because he was not sure when and why to use the quotation marks. I hope I have been helpful.

Hi folks,

The rules on quoting are determined by JavaScript versus JSON syntax. The two are often used almost synonymously but “JSON” examples for the MongoDB shell are typically JavaScript (or loose JSON) rather than standards-compliant JSON.

I agree with @steevej’s suggestion: the habit of always using quotes will mean you are less likely to encounter a syntax error in different contexts. However, if you know the syntax rules for the current execution context, code may look a tad cleaner (or be easier to type) without optional quotes.

JavaScript syntax (for example, in the MongoDB shell)

Field names or operators appearing on the key side of a key:value pair must either be valid JavaScript identifiers or quoted to avoid a syntax error:

An identifier is a sequence of characters in the code that identifies a variable, function, or property.

In JavaScript, identifiers are case-sensitive and can contain Unicode letters, $ , _ , and digits (0-9), but may not start with a digit.

Unquoted keys like { $project: { magic: 1 }} are valid in a JavaScript context such as the MongoDB shell as long as they meet the JavaScript identifier rules.

A key name including spaces or a leading digit has to be quoted or it will result in a syntax error:

var trythis = { $project: { 4magic : 1 }}
uncaught exception: SyntaxError: identifier starts immediately after numeric literal

var trythis = { $project: { for the win : 1 }}
uncaught exception: SyntaxError: missing : after property id

JSON (JavaScript Object Notation) syntax (for example, mongodump --query=<JSON>)

JSON is a data interchange format that was derived from JavaScript but has stricter rules for standards compliance.

In particular, strict JSON syntax requires quoting field names so parsers in languages other than JavaScript do not have to support the quirks of JavaScript identifiers:

A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value.

Trying my first example in a JSON lint checker:

{ { $project: { 4magic : 1 }}
Error: Parse error on line 1:
{ $project: { magic:
Expecting ‘STRING’, ‘}’, got ‘undefined’

The expected JSON syntax is to have all keys quoted: { "$project": { "magic": 1 }}.



I respect the OPINION of each one, really. But I still think it is better to LEARN where and why quotes are necessary.
Even if this makes you suffer a little, it is worth it.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.