BSON stands for Binary JSON. It is a binary file format that is used to store serialized JSON documents in a binary-encoded format. It was developed in 2009 by MongoDB. The MongoDB database had several scalar data formats that were of special interest only for MongoDB, hence they developed the BSON data format to be used while transferring files over the network. Although the format was developed specifically for MongoDB, it can be used anywhere as per business requirements independently.
It has several similarities with JSON for instance BSON too supports nested documents and arrays within other documents, but yet has a lot of striking differences. Refer to this post to read more about the difference between JSON and BSON.
Sample BSON document
Consider the following JSON document:
{ "hello" : "world" }
It’s BSON equivalent will be:
\x16\x00\x00\x00 // Size of the Document \x02 // 0x02 = type String hello\x00 // field name \x06\x00\x00\x00world\x00 // field value \x00 // Used to represent end of object
where “\x01” is used to represent 0000 0001.
Characteristics of BSON Documents: Following are the three main characteristics of BSON documents-
- Lightweight – The term overhead during data transmission refers to the extra bits that are not necessary for transmission (like checksum, parity etc.). These bits are transferred along with data usually as a part of headers. BSON keeps spatial overhead to a bare minimum which allows it to be lightweight during transfer over any network.
- Traversable – BSON was designed to be highly traversable in nature. It can be parsed easily and very quickly because it supports type and length encoding, thanks to its binary structure.
- Efficient – BSON supports efficient data encoding and decoding. Data can be both encoded to and decoded from BSON very easily and quickly.
Why BSON?
Following are the advantages of using BSON during data transfer –
- BSON can be parsed easily and very quickly because it supports type and length encoding, thanks to its binary structure.
- BSON objects are designed to be highly traversable and lightweight in nature, which makes it a better option for data transfer.
- It takes up less space and offers faster scan speed as compared to JSON objects.
- It offers a wide range of data types, like – date type, etc.), many of which are not supported by JSON.
Data Types supported by BSON: Following are the data types supported by BSON –
S No | Data Type | Size (bytes) |
1 | byte | 1 |
2 | int32 | 4 |
3 | int64 | 8 |
4 | uint64 | 8 |
5 | double | 8 |
6 | decimal128 | 16 |
7 | date | 8 |
8 | objectId | 12 |
9 | array | Based on data (For ex – A byte array uses 1 byte) |
Disadvantages of using BSON: BSON supports fast traversal of the BSON document. In order to support it, BSON adds extra information (like the length of subobjects, etc.) to the document. In some cases, this leads to increase in the size of the document and decreases the efficiency when compared to JSON.