JSON vs BSON Data Types & MongoDB Internal Storage Engine (WiredTiger)
JSON vs BSON Data Types & MongoDB Internal Storage Engine (WiredTiger)
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:
- 1Limited 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.
- 2Text Parsing Overhead: Text JSON requires parsing strings character-by-character on every query, which is computationally expensive.
- 3No 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, andBinData.
2. Anatomy of the MongoDB ObjectId
Every MongoDB document requires a unique primary key _id. If not provided, MongoDB automatically generates a 12-byte ObjectId:
- Built-in Timestamp: You can extract the exact creation timestamp of any document using
doc._id.getTimestamp()without storing an extracreatedAtfield! - 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.
Multiple Choice Questions
ObjectId occupy in memory?_id ObjectId without querying any other field?MongoDB Atlas Cloud Setup, Network Whitelisting & Compass GUI
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.