BSON Overview

BSON (Binary JSON) is a binary storage format that extends JSON’s data representation capabilities.

Data Type Support

BSON adds multiple special data types on top of JSON’s native data types:

  • Date type: Used to store datetime information
  • Binary Data type: Used to store binary data
  • ObjectId: MongoDB-specific unique identifier

Basic Characteristics

  1. Lightweight

    • Compact binary encoding
    • Particularly suitable for network transmission and disk storage
  2. Traversability

    • Retains hierarchical structure similar to JSON
    • Built-in length prefix mechanism
  3. Efficiency

    • Encoding/decoding speed 5-10x faster than text JSON
    • Supports native representation of multiple data types

BSON Types

  1. Double: 64-bit floating point
  2. String: UTF-8 encoded string
  3. Object: Nested BSON document
  4. Array: Ordered collection of values
  5. Binary data: Arbitrary binary data
  6. ObjectId: Default unique identifier for MongoDB documents
  7. Boolean: true or false
  8. Date: 64-bit integer representing milliseconds since Unix epoch
  9. Null: Represents null value
  10. Integer: 32-bit signed integer
  11. Timestamp: Special internal type
  12. Long: 64-bit signed integer
  13. Decimal128: 128-bit decimal floating point
  14. Regular Expression: Regular expression

Use Cases

  1. MongoDB database’s default data storage format
  2. Network data exchange format
  3. Binary data serialization