Open In App

BSON Types - MongoDB

Last Updated : 18 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

BSON (Binary JSON) is the data storage format used by MongoDB to represent documents in a highly efficient and flexible manner. It extends JSON by supporting additional data types, optimizing storage, and improving query performance. Unlike standard JSON, BSON allows MongoDB to handle complex data structures, including ObjectId, Decimal128, Binary, and more.

Understanding BSON types is essential for designing effective schemas, optimizing indexing, and improving data retrieval in MongoDB. This article explores BSON types in detail, explaining their significance, usage, and corresponding MongoDB queries with explanations..

What is BSON?

BSON (Binary JSON) is a binary-encoded format that MongoDB uses to store documents. It is designed to be lightweight, traversable, and efficient. BSON offers several advantages over JSON, such as:

  • Support for additional data types (e.g., ObjectId, Date, Decimal128, Binary, etc.)
  • Efficient storage and retrieval due to its binary structure
  • Indexing support for data types like numbers, dates, and arra

BSON Data Types in MongoDB

MongoDB supports various BSON data types that can be used when defining documents. Below are the key BSON types with queries and their explanations.

1. String (string)

  • Used for storing textual data.
  • In MongoDB, strings are UTF-8 encoded.

Query: Insert a document with a string field

db.users.insertOne({ "name": "John Doe" })

Explanation: This query inserts a document into the users collection with a field "name" storing a string value "John Doe".

Query: Find a document based on a string value

db.users.find({ "name": "John Doe" })

Explanation: This query retrieves documents from the users collection where the "name" field is exactly "John Doe".

2. Integer (int32 and int64)

  • Ued for storing whole numbers.
  • MongoDB supports 32-bit and 64-bit integers.

Query: Insert an integer value

db.users.insertOne({ "age": 30 })

3. Double (double)

Used for storing floating-point numbers.

Query: Insert a document with a floating-point number

db.products.insertOne({ "price": 19.99 })

Explanation: Inserts a product into the products collection with a "price" field storing the floating-point value 19.99.

4. Decimal (decimal128)

High-precision decimal type used for financial applications.
Query: Insert a high-precision decimal value

de b.salaries.insertOne({ "salary": NumberDecimal("1000.50") })

Explanation: Inserts a document into the salaries collection with "salary" stored as a high-precision decimal value.

5. ObjectId (ObjectId)

A 12-byte unique identifier generated by MongoDB for each document.
Query: Find a document by its ObjectId

db.users.find({ "_id": ObjectId("60d5b2b3c3a2f34f1c4d2f48") })

Explanation: Retrieves a document where the _id field matches the given ObjectId.

6. Boolean (bool)

Used for storing true or false values.
Query: Insert a boolean value

db.users.insertOne({ "isActive": true })

Explanation: Inserts a document into the users collection with a boolean field "isActive" set to true.

7 Timestamp (timestamp)

Special timestamp type used for MongoDB internal operations.

Query: Insert a timestamp

db.logs.insertOne({ "logTime": Timestamp() })

Explanation: Inserts a document into the logs collection with a "logTime" field storing the current timestamp.

8. Regular Expression (regex)

Used for pattern matching.

Query: Find names starting with "J"

db.users.find({ "name": /^J/ })

Explanation: Retrieves users whose "name" starts with "J" using a regex pattern.

Conclusion

BSON plays a critical role in MongoDB's data storage and retrieval processes. With its support for various data types beyond JSON, such as ObjectId, Decimal128, and Timestamp, BSON enhances MongoDB's flexibility and efficiency. By understanding and utilizing the appropriate BSON types, developers can optimize their database schema, ensure efficient indexing, and improve overall application performance.

Mastering BSON types not only helps in storing and retrieving data effectively but also enhances query efficiency and ensures data integrity. As MongoDB continues to evolve, leveraging BSON's full potential is key to building scalable and high-performance applications.


Next Article
Article Tags :

Similar Reads