Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 543
Vladimir Avatar asked Nov 24 '25 08:11

Vladimir


1 Answers

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)

like image 112
ibininja Avatar answered Nov 26 '25 21:11

ibininja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!