javascriptjsonmongodb

No quotes in MongoDB JSON


I am learning MongoDB. While JSON specification is like:

{"Website":"Stack overflow", "Popularity":"High"}

in Mongo JavaScript examples I see it like:

{
name: "David",
score: 0
}

I have tried the following example:

text = '{"name": "Wallie"}';
text2 ='{name: "Wallie"}' 

JSON.parse(text) works well while JSON.parse(text2) gives Syntax Error: Unexpected token.

So why MongoDBs JSON is different?


Solution

  • My understanding is the following:

    The following

    {"Website":"Stack overflow", "Popularity":"High"} 
    

    is the complete way to create the queries with quotations over the keys and values.

    This example

    {name: "David",score: 0}
    

    is acceptable if you are using mongo shell since quotes are implicit (again in mongo shell) and can be omitted; the shell will handle it for you. If you are not using the shell Quotes should be there as per JSON specs.

    My advice is that to start with mongodb start from the shell then move out to any programming language that is supported by their drivers (java, python, js..etc)