0
0
MongodbComparisonBeginner · 4 min read

JSON vs BSON in MongoDB: Key Differences and Usage

In MongoDB, JSON is a text-based format used for data interchange, while BSON is a binary format optimized for storage and speed. MongoDB stores data internally as BSON to allow efficient encoding, decoding, and support for additional data types beyond JSON.
⚖️

Quick Comparison

This table summarizes the main differences between JSON and BSON in MongoDB.

AspectJSONBSON
Format TypeText-basedBinary
Data SizeLarger due to text encodingSmaller and more compact
Supported Data TypesBasic types (string, number, boolean, array, object)Extended types (date, binary, int32, int64, decimal128, etc.)
Read/Write SpeedSlower parsing and serializationFaster due to binary encoding
Usage in MongoDBUsed for data interchange and human readabilityUsed internally for storage and network transfer
Human ReadabilityEasy to read and editNot human-readable
⚖️

Key Differences

JSON (JavaScript Object Notation) is a simple, text-based format that is easy for humans to read and write. It supports basic data types like strings, numbers, arrays, and objects but lacks support for some complex types such as dates or binary data.

BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents. It extends JSON by adding support for additional data types like Date, Binary, Int32, Int64, and Decimal128. This makes BSON more suitable for database storage and efficient data transfer.

MongoDB uses BSON internally because it allows faster encoding and decoding compared to JSON. BSON's binary format reduces the size of data and improves performance when reading and writing documents. However, for communication with applications or users, JSON is often used because it is easier to understand and debug.

⚖️

Code Comparison

Here is an example of a document represented in JSON format:

json
{
  "name": "Alice",
  "age": 30,
  "isMember": true,
  "joined": "2023-01-15T00:00:00Z",
  "scores": [85, 90, 88]
}
Output
{ "name": "Alice", "age": 30, "isMember": true, "joined": "2023-01-15T00:00:00Z", "scores": [85, 90, 88] }
↔️

BSON Equivalent

The same document in BSON format (shown here conceptually, as BSON is binary and not human-readable):

javascript
const BSON = require('bson');

const doc = {
  name: "Alice",
  age: 30,
  isMember: true,
  joined: new Date('2023-01-15T00:00:00Z'),
  scores: [85, 90, 88]
};

const bsonData = BSON.serialize(doc);
console.log(bsonData);
Output
<Buffer 16 00 00 00 02 6e 61 6d 65 00 06 00 00 00 41 6c 69 63 65 00 10 61 67 65 00 1e 00 00 00 08 69 73 4d 65 6d 62 65 72 00 01 09 6a 6f 69 6e 65 64 00 00 00 00 00 00 00 00 00 00 00 00 04 73 63 6f 72 65 73 00 10 00 00 00 10 00 55 00 00 10 01 5a 00 00 10 02 58 00 00 00 00>
🎯

When to Use Which

Choose JSON when you need a format that is easy to read, write, and debug, especially for configuration files, APIs, or data interchange between systems.

Choose BSON when working directly with MongoDB storage or network transfer, as it offers better performance, supports more data types, and reduces data size.

In practice, MongoDB drivers handle conversion between JSON and BSON automatically, so developers mostly interact with JSON-like syntax while benefiting from BSON's efficiency internally.

Key Takeaways

MongoDB stores data internally as BSON for efficiency and extended data type support.
JSON is text-based and human-readable but less efficient for storage and speed.
BSON supports more data types like dates and binary data that JSON does not.
Use JSON for easy data interchange and debugging, BSON for database storage and performance.
MongoDB drivers convert between JSON and BSON automatically for developer convenience.