The Journey of #100DaysOfCode (Baqer_Qabalan)

Day2: How MongoDB leverages BSON?

BSON (Binary JSON) is a binary-encoded serialization format that is used to store documents and make remote procedure calls in MongoDB. It extends the JSON model to provide additional data types and to be efficient for encoding and decoding within different languages.

Consider the following JSON document:

{
  "name": "John Doe",
  "age": 30,
  "address": {
    "street": "123 Elm St",
    "city": "Somewhere",
    "state": "CA"
  },
  "phones": ["123-456-7890", "987-654-3210"],
  "isActive": true
}

In BSON:
\x16\x00\x00\x00\x02name\x00\x08\x00\x00\x00John Doe\x00\x10age\x00\x1e\x00\x00\x00\x03address\x00\x3d\x00\x00\x00\x02street\x00\x09\x00\x00\x00123 Elm St\x00\x02city\x00\x09\x00\x00\x00Somewhere\x00\x02state\x00\x03\x00\x00\x00CA\x00\x00\x04phones\x00\x1f\x00\x00\x00\x020\x00\x0c\x00\x00\x00123-456-7890\x0001\x00\x0c\x00\x00\x00987-654-3210\x00\x00\x08isActive\x00\x01\x00\x00\x00

As a result:
Using BSON, MongoDB combines the flexibility of JSON with the efficiency of binary encoding, making it a robust solution for handling large and complex datasets.
100daysofcode lebanon-mug

3 Likes