Script Valley
MongoDB: Complete Course
MongoDB FoundationsLesson 1.3

How MongoDB BSON and ObjectId work under the hood

BSON data types, ObjectId structure, timestamp encoding, _id field, supported types vs JSON, binary serialization

BSON extends JSON with richer types

JSON vs BSON types

MongoDB stores documents as BSON — Binary JSON. While the MongoDB shell and drivers accept and display JSON-like syntax, all data is serialized to BSON on disk. BSON adds types that JSON cannot represent natively: Date, ObjectId, Int32, Int64, Decimal128, and Binary. This matters when you query date ranges or work with large integers that would lose precision as floating-point numbers.

ObjectId anatomy

Every document requires a unique _id. If omitted, MongoDB auto-generates an ObjectId — a 12-byte value containing a 4-byte Unix timestamp, a 5-byte random machine identifier, and a 3-byte incrementing counter to prevent same-second collisions.

const id = ObjectId("64a1f2c3d4e5f6a7b8c9d0e1")

// Extract creation timestamp from the first 4 bytes
id.getTimestamp()  // ISODate("2023-07-02T10:00:03.000Z")

// Provide your own _id — any unique value works
db.users.insertOne({ _id: "user_ada_001", name: "Ada" })

// Generate and inspect a fresh ObjectId
const fresh = ObjectId()
fresh.getTimestamp() // approximately now

Why ObjectId ordering matters

Because the first 4 bytes are a Unix timestamp, sorting documents by _id approximates insertion order with no separate createdAt field needed. Duplicate _id values always throw error code 11000, whether auto-generated or supplied manually by the application.

Up next

MongoDB schema design: embedding vs referencing documents

Sign in to track progress