Recently I came across a statement that said: “MongoDB (btw. that’s MongoDB) uses the BSON format which extends the JSON model to provide additional data types” and I think this topic deserves a bit of clarification.
The statement is correct, MongoDB does store the data records (documents) in BSON format. I just need to add that that this is only happening in the background. MongoDB uses standard JSON documents when you work with it. It’s only at the time of storing the records in the database that MongoDB would switch to BSON. I wanted to make this distinction clear, that it’s not that you’d work with MongoDB in BSON format. No, you’d still use JSON to index and retrieve documents, but MongoDB would save those documents entered in a JSON format, as a binary-encoded BSON type behind the scenes.
BSON is essentially a binary representation of JSON, which gives MongoDB little more functionality than it would get with default JSON format, which is illustrated in Figure 1.
Why MongoDB needs to use BSON?
They can afford to do this behind the scene implementation of BSON without losing any performance because the format is very lightweight even when used over the network and similarly to JSON it can be traversed very easily. BSON specification also says that the format is very efficient, I quote: “Encoding data to BSON and decoding from BSON can be performed very quickly in most languages due to the use of C data types” (BSON – binary JSON, 2017).
MongoDB says that BSON allows them to “extends the JSON model to provide additional data types, ordered fields, and to be efficient for encoding and decoding within different languages” (MongoDB, 2017), but the main reason is that MongoDB’s BSON implementation gives them the ability to embed objects and arrays within other objects and arrays. By using BSON, MongoDB is capable of doing some fascinating things, such as constructing indexes and match objects against query expressions on both top-level and nested BSON keys, which means that MongoDB gives users the ease of use and flexibility of JSON documents together with the speed and richness of a lightweight binary format.
To conclude, I wanted to add one more note. The MongoDB was the first large project to make use of BSON and there is already a plan to release the libraries that deal with BSON in a more stand-alone way, the expectations are that those libraries could then be used independently of MongoDB.
MongoDB (2017) JSON and BSON. Available at: https://www.mongodb.com/json-and-bson (Accessed: 30 January 2017).
BSON – binary JSON (2017) Available at: http://bsonspec.org/ (Accessed: 30 January 2017).