While developers query MongoDB using familiar JavaScript/JSON notation, the database internally serializes and stores documents as BSON (Binary JSON). Behind the scenes, the WiredTiger storage engine delivers enterprise-grade performance, in-memory caching, and transparent disk compression.
1. Why Not Pure JSON? (JSON vs BSON)
Plain JSON (JavaScript Object Notation) is a lightweight text-based format, but it has severe limitations for a production database:
1
Limited Data Types: JSON supports only strings, numbers, booleans, arrays, objects, and null. It lacks native representations for Dates, 64-bit integers, regular expressions, and raw binary bytes.
2
Text Parsing Overhead: Text JSON requires parsing strings character-by-character on every query, which is computationally expensive.
3
No Traversal Offsets: Finding a field in a large nested JSON document requires scanning the entire string up to that character.
BSON (Binary JSON) solves these issues:
Binary Encoding: Compact binary representation with prefixed length bytes for fast traversal.
Rich Type Support: Native ISODate, ObjectId, Decimal128 (high-precision financial arithmetic), Int64, and BinData.
Built-in Timestamp: You can extract the exact creation timestamp of any document using doc._id.getTimestamp() without storing an extra createdAt field!
Natural Ordering: ObjectIds are roughly ordered chronologically.
3. The WiredTiger Storage Engine
MongoDB's default storage engine, WiredTiger, is responsible for managing data between memory and disk:
Document-Level Concurrency: Provides fine-grained document-level locking for write operations, maximizing multi-core CPU throughput.
In-Memory LRU Cache: Uses 50% of available server RAM minus 1 GB by default to cache hot indexes and documents.
Snappy / Zlib Compression: Transparently compresses collection data and indexes on disk, typically saving 50% to 70% of storage costs compared to raw JSON.
Write-Ahead Logging (Journaling): Commits write operations to an on-disk journal before flushing to data files, guaranteeing durability across sudden server power outages.
Interactive Quiz• Knowledge Check
Multiple Choice Questions
Question 1
What is BSON in MongoDB?
Question 2
How many bytes does a standard MongoDB ObjectId occupy in memory?
Question 3
What can be extracted directly from a document's default _id ObjectId without querying any other field?
Question 4
What type of concurrency locking does the WiredTiger storage engine provide for write operations?