Reputation: 87
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?
Upvotes: 3
Views: 1567
Reputation: 522081
in Mongo JavaScript examples I see it like
This is the decisive factor. What exactly are you looking at?
JSON is a data interchange format represented as a specifically formatted string. Mongo doesn't even really use JSON, it uses BSON under the hood. You probably never really get to see BSON directly anywhere. What you're looking at most of the time is a Mongo document represented as some native object in your language of choice.
Mongo documents are "dictionaries" with keys and values. This concept exists in virtually any language, with varying details. In Javascript a key-value object looks like this:
{ key: 'value' }
In Python it may look like this:
dict(key='value')
# or
{'key': 'value'}
In PHP it'd look like this:
['key' => 'value']
Conceptually these are all the same things, with language-specific implementations.
JSON is a string format which aims to express data such that it can be interchanged between all these languages. In JSON, the above data structures would look like:
{"key": "value"}
This is not Javascript, not Python, nor PHP, even if it looks very similar to some of those and is actually valid in some of those. JSON was inspired by the Javascript syntax, but it's not Javascript.
Let's use Javascript for illustrative purposes:
let json = '{"key": "value"}';
let obj = { key: 'value' };
The JSON value is a string. The native Javascript object is a Javascript object. You can turn the former into the latter by JSON-decoding it:
obj = JSON.parse(json);
When working with Mongo, you basically don't ever see JSON. You see native objects of whatever language you're working with, which you pass to a function of the mongo library/driver. That library encodes the object to JSON/BSON for you, and sends it to the actual Mongo database, where it is stored. When retrieving data again from the database, the library is decoding it to a native object again.
When using some GUIs to look at the data in the database, the same thing happens: you see some interpreted representation of whatever the database actually stores. It may look JSON-ish, or be represented as actual JSON, or an interactive tree you can expand and collapse, or a table. Probably at no point are you actually looking at the underlying BSON directly.
Saying Mongo works with JSON is a confusing shorthand. Even Mongo themselves don't say that, they say:
MongoDB documents are similar to JSON objects.
https://www.mongodb.com/docs/manual/introduction/#document-database
(Emphasis mine.)
At best JSON is an intermediate representation which may be used somewhere by the database driver between your code and the actual database. But you basically never see it directly. "JSON" is mostly a marketing term, since many people know JSON, and can conceptually understand what it means to store "a JSON document" easily.
Upvotes: 1
Reputation: 1217
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)
Upvotes: 2
Reputation: 87
Finally I have found solution at https://docs.mongodb.org/manual/core/document/#document-format
It's BSON not JSON, which MongoDB uses. It resembles JavaScript object notation and is compatible with JSON, hence the confusion.
Upvotes: 0