MongoDB Bson Tutorial: What is BSON and How is It Different from JSON?
BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents used in MongoDB databases. This article provides an overview of BSON, the binary data format used by MongoDB to store documents.
BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents used in MongoDB databases. This article provides an overview of BSON, the binary data format used by MongoDB to store documents.
It explains what BSON is, how it extends JSON capabilities and key differences between BSON and JSON. In addition, it includes how Apidog handles this automatically. It also shows examples of basic CRUD operations and database commands using Apidog to interact with MongoDB.
What is Bson in MongoDB?
BSON (Binary JSON) is the primary data format used in MongoDB databases. Some key things to know about BSON in MongoDB:
- BSON is a binary representation of JSON documents. It extends JSON with additional data types like dates, timestamps, binary data, etc.
- MongoDB stores all data internally as BSON. When you insert a document into MongoDB, it is first converted to BSON before being written to disk.
- BSON encodes type and length information, allowing MongoDB to efficiently scan and traverse documents without examining and interpreting each one. This improves performance.
- BSON is designed to be lightweight, traversable, and efficient to encode/decode. This makes it well-suited as a data storage and interchange format for MongoDB.
- The default file extension for BSON files is .bson. Mongo tools like Mongoexport can be used to export MongoDB data to BSON for backups or migrations.
- BSON is designed to be highly traversable. MongoDB queries utilize indexes to quickly locate and retrieve results without scanning every document.
Structure of a BSON Document
BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents used in MongoDB databases. A BSON document contains ordered field-value pairs similar to JSON objects.
The fields can be any valid UTF-8 strings, while values can be one of several BSON data types like string, integer, double, binary data, array, document, boolean, date, null, etc. The structure of a BSON document starts with a 4-byte integer indicating the total document size.
Next is a field-value pair containing the "_id" field which uniquely identifies the document. The document content follows with each field-value pair encoded in a specific binary format based on the data type.
An example showing the structure of a BSON document:
// Sample BSON document
{
_id: ObjectId("507f1f77bcf86cd799439011"), // 12-byte unique ID
name: "John Doe", // String
age: 35, // Integer
address: { // Embedded document
street: "123 Main St",
city: "Anytown",
state: "CA"
},
hobbies: ["reading", "hiking","swimming"], // Array
graduated: true, // Boolean
birthday: ISODate("1980-05-15T00:00:00Z"), // Date
data: BinData(0, "SGVsbG8gV29ybGQ="), // Binary data
ts: Timestamp(1590583045633, 1) // Timestamp
}
Key things to note:
_id
field uniquely identifies the document- Field-value pairs have a defined order
- Supports extra data types like ISODate, ObjectId, etc
- Can embed documents and arrays
- Binary data stored efficiently
What is the difference between JSON and BSON?
Here are the main differences between JSON and BSON:
- Encoding: JSON is text-based and encoded in UTF-8, while BSON is binary-encoded.
- Data types: JSON supports limited data types like strings, numbers, booleans, arrays, and objects. BSON supports additional types like dates, binary data, regular expressions, etc.
- Efficiency: The binary BSON encoding allows for more efficient storage and lookup of data compared to JSON. BSON documents have a length prefix and contain no whitespace so they can be parsed faster.
- Ordering: The field-value pairs in BSON documents are ordered allowing efficient traversal and indexing. JSON objects have no ordering guarantees.
- Flexibility: BSON provides some flexibility over JSON such as allowing field names to contain special characters and Unicode.
- Usage: JSON is a general format used widely across the web and applications. BSON is used specifically in MongoDB databases as the primary data representation.
- Schema: JSON is schema-less by default while MongoDB allows enforcing schemas for BSON documents through validation rules.
- Null values: JSON has a single null value. BSON distinguishes between null and missing fields.
Conversion JSON and BSON in MongoDB with Apidog
Apidog is an API toolkit that simplifies API development. It offers MongoDB as one of its integrated database choices. By using MongoDB with Apidog, you can store API mocks and tests in MongoDB as the backend.
The mock API responses delivered by Apidog can be driven by data stored in a MongoDB database. This allows Apidog to leverage MongoDB as the persistent data source for mocked APIs. Next, we will explain the relation of JSON and BSON with you.
MongoDB stores data as BSON documents, which can be seen as a superset of JSON. When operating MongoDB database with Apidog, JSON is used to compose the content. Apidog will automatically map each field to the corresponding BSON data type based on the actual JSON content.
One special case is the _id
field. According to MongoDB conventions, each document must have an _id
field as the primary key, with ObjectId
as the default data type instead of string
.
With Apidog, to declare an _id
field of ObjectId
type, use the regular string format. If the string content conforms to the ObjectId
format, Apidog will automatically map it to BSON's ObjectId
type.
For example, in MongoDb there is a BSON document like:
Copy code
{
_id: ObjectId('654e056de3662b1c09477cc3
'),
name: "Apidog"
}
Then to query this document by _id
with Apidog, the JSON to put in the "Query Condition" would be:
Copy code
{
"_id": "654e056de3662b1c09477cc3"
}