ChatGPT解决这个技术问题 Extra ChatGPT

What is BSON and exactly how is it different from JSON?

I am just starting out with MongoDB and one of the things that I have noticed is that it uses BSON to store data internally. However the documentation is not exactly clear on what BSON is and how it is used in MongoDB. Can someone explain it to me, please?


J
JohnnyHK

BSON is the binary encoding of JSON-like documents that MongoDB uses when storing documents in collections. It adds support for data types like Date and binary that aren't supported in JSON.

In practice, you don't have to know much about BSON when working with MongoDB, you just need to use the native types of your language and the supplied types (e.g. ObjectId) of its driver when constructing documents and they will be mapped into the appropriate BSON type by the driver.


So if mongoDb stores documents as bson what is the type that is returned when we query the data base?Are they returned as json?Or bson is returned as it is?
No, the MongoDB driver for your language takes care of converting the document into data types appropriate for your language. Native data types are used as much as possible.
Does that means, if i retrieve data from BSON document using Javascript, i won't get any datatype difference as i'll get when i retrieve using C/C++ i.e. numbers will be treated as integers if it doesn't have any decimal?
@abhisekp based on JohnnyHK's explanation and my recent experience, I think what you said is true.
Will the BSON return the exact data type of the column field ? Or either it return it as String for all the types ?
N
Nicola

What's BSON? BSON [bee · sahn], short for Binary JSON, is a binary-encoded serialization of JSON-like documents.

How is it different from JSON? BSON is designed to be efficient in space, but in some cases is not much more efficient than JSON. In some cases BSON uses even more space than JSON. The reason for this is another of the BSON design goals: traversability. BSON adds some "extra" information to documents, like length of strings and subobjects. This makes traversal faster. BSON is also designed to be fast to encode and decode. For example, integers are stored as 32 (or 64) bit integers, so they don't need to be parsed to and from text. This uses more space than JSON for small integers, but is much faster to parse. In addition to compactness, BSON adds additional data types unavailable in JSON, notably the BinData and Date data types.

Source: http://bsonspec.org/


is this the same as the data type jsonb which stands for binary JSON data, decomposed in postgresql?
F
Fouad Boukredine

MongoDB represents JSON documents in binary-encoded format so we call it BSON behind the scenes.

BSON extends the JSON model to provide additional data types such as Date and binary which are not supported in JSON also provide ordered fields in order for it to be efficient for encoding and decoding within different languages.

In other word we can say BSON is just binary JSON ( a superset of JSON with some more data types, most importantly binary byte array ).

Mongodb using as a serialization format of JSON include with encoding format for storing and accessing documents. simply we can say BSON is a binary encoded format for JSON data.

for more mongoDB Article : https://om9x.com/blog/bson-vs-json/


the link is broken
... and the answer is off-axis. BSON is only superficially similar to JSON, and that's when it is rendered toString().
c
codergirl22

MongoDB represents JSON documents in binary-encoded format called BSON behind the scenes. BSON extends the JSON model to provide additional data types and to be efficient for encoding and decoding within different languages.


A
Anupam Mahapatra

By using BSON encoding on top of JSON, MongoDB gets the capability of creating indexes on top of values that resides inside the JSON document in raw format. This helps in running efficient analytical queries as NoSQL system were known for having no support for Indexes.


H
HairOfTheDog

This relatively short article gives a pretty good explanation of BSON and JSON: It talks about some of the problems with JSON, why BSON was invented, what problems it solves compared to JSON and how it could benefit you.

https://www.compose.com/articles/from-json-to-bson-and-back/

In my use case that article told me that serializing to JSON would work for me and I didn't need to serialize to BSON


B
Buzz Moschetti

To stay strictly within the boundaries of the OP question:

What is BSON?

BSON is a specification for a rich set of scalar types (int32, int64, decimal, date, etc.) plus containers (object a.k.a. a map, and array) as they might appear in a byte stream. There is no "native" string form of BSON; it is a byte[] spec. To work with this byte stream, there are many native language implementations available that can turn the byte stream into actual types appropriate for the language. These are called codecs. For example, the Java implementation of a BSON codec into the Document class from MongoDB turns objects into something that implements java.util.Map. Dates are decoded into java.util.Date. Transmitting BSON looks like this in, for example, Java and python:

Java:
import org.bson.*;
MyObject  -->  get() from MyObject, set() into org.bson.Document --> org.bson.standardCodec.encode(Document) to byte[]

XMIT byte[]

python:
import bson
byte[] --> bson.decode(byte[]) to dict --> get from dict --> do something

There are no to- and from- string calls involved. There is no parser. There is nothing about whitespace and double quotes and escaped characters. Dates, BigDecimal, and arrays of Long captured on the Java side reappear in python as datetime.datetime, Decimal, and array of int.

In comparison, JSON is a string. There is no codec for JSON. Transmitting JSON looks like this:

MyObject --> convert to JSON (now you have a big string with quotes and braces and commas)

XMIT string

parse string to dict (or possibly a class via a framework) 

Superficially this looks the same but the JSON specification for scalars has only strings and "number" (leaving out bools and nulls, etc.). There is no direct way to send a long or a BigDecimal from sender to receiver in JSON; they are both just "number". Furthermore, JSON has no type for plain byte array. All non-ASCII data must be base64 or otherwise encoded in a way to protect it and sent as a string. BSON has a byte array type. The producer sets it, the consumer gets it. There is no secondary processing of strings to turn it back into the desired type.

How does MongoDB use BSON?

To start, it is the wire protocol for content. It also is the on-disk format of data. Because varying length types (most notably string) carry length information in the BSON spec, this permits MongoDB to performantly traverse an object (hopping field to field). Finding the object in a collection is more than just BSON including use of indexes.