JSON vs BSON in MongoDB
When working with MongoDB, you'll often hear about both JSON and BSON. JSON is a text-based format commonly used for data exchange in web applications, while BSON is the binary representation of that data used internally by MongoDB.
What is JSON?
JSON stands for JavaScript Object Notation. It is easy for humans to read and write, and easy for machines to parse and generate. JSON is text-based and often used in APIs, configuration files, and for sending data across networks.
Example JSON:
{
"name": "Alice",
"age": 30,
"hobbies": ["reading", "hiking"]
}
What is BSON?
BSON stands for Binary JSON. It is a binary-encoded format used by MongoDB to store data on disk and in memory. BSON extends JSON by adding support for more data types such as Date
, Binary
, and ObjectId
.
Key Differences:
- BSON is binary, JSON is plain text
- BSON supports more data types (e.g., Date, ObjectId)
- BSON is faster to parse and encode for MongoDB
- BSON can be more space-efficient for numeric data
Why does MongoDB use BSON?
BSON was designed to be efficient for encoding and decoding data in MongoDB. Since MongoDB deals with large volumes of data and needs fast read/write performance, a binary format like BSON is ideal.
Beginner Intuition
Q: Can you send data to MongoDB in JSON format?
A: Yes, when you interact with MongoDB (e.g., via drivers or the shell), you often write data in JSON format. MongoDB converts this JSON into BSON internally for storage and processing.
Example: Inserting JSON (Stored as BSON)
db.users.insertOne({
name: "Bob",
age: 28,
email: "bob@example.com",
joined: new Date()
});
Output:
{ acknowledged: true, insertedId: ObjectId("645f76c2b8a2f3c872d6b821") }
Explanation: You wrote the data in JSON format. MongoDB stored it as BSON. Notice that the joined
field is stored as a BSON Date
type, and the _id
is an ObjectId
— both of which JSON doesn't natively support.
Viewing BSON as Extended JSON
When you retrieve documents from MongoDB, BSON is converted back to a readable format using Extended JSON. For example:
db.users.find({ name: "Bob" });
Output:
{ _id: ObjectId("645f76c2b8a2f3c872d6b821"), name: "Bob", age: 28, email: "bob@example.com", joined: ISODate("2023-05-15T10:30:00Z") }
Explanation: The date is shown as ISODate
, which is MongoDB’s extended JSON representation of BSON’s Date
type. The _id
is an ObjectId
— a 12-byte BSON type that ensures uniqueness.
Example: JSON Limitation vs BSON Capability
Let’s try to insert a field with binary data (e.g., image buffer). JSON doesn’t support it, but BSON does:
db.files.insertOne({
name: "image.png",
data: new BinData(0, "aGVsbG8gd29ybGQ=")
});
Output:
{ acknowledged: true, insertedId: ObjectId("645f7788b8a2f3c872d6b823") }
Explanation: Here, the binary data is stored using BSON’s BinData
type. You cannot do this directly in JSON — another reason why MongoDB uses BSON behind the scenes.
Command: Export Data as JSON
You can still export MongoDB data in JSON using mongoexport
:
mongoexport --db=test --collection=users --out=users.json --jsonArray
Explanation: This command exports the users
collection into a JSON file. Data will be converted from BSON to Extended JSON for compatibility.
Summary
- JSON is used to input and read data
- BSON is used internally for efficient storage and supports extra types
- MongoDB handles the conversion automatically — you mostly interact with JSON, but the real magic is in BSON
What’s Next?
Now that you understand how MongoDB stores data using BSON while letting you work with JSON, let's dive into how to create databases and collections in MongoDB.