$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[July 10, 2025] MongoDB

MongoDB ObjectId: Generation Strategies

MongoDB ObjectId: Generation Strategies

────────────────────────────────────────────────────────

MongoDB ObjectId: Generation Strategies

Tip: ObjectId Structure

ObjectId is 12 bytes: 4-byte timestamp + 5-byte random value + 3-byte counter.

Gotcha: Extract Timestamp from ObjectId

$id = new MongoDB\BSON\ObjectId('507f191e810c19729de860ea');
$timestamp = $id->getTimestamp();

The ID encodes its creation time.

Tip: Custom ID Generation

$post = new Post();
$post->_id = new MongoDB\BSON\ObjectId();
$post->save();

Generate IDs client-side before insert.

Gotcha: String IDs vs ObjectId

If you use string IDs, you lose the built-in timestamp and sorting benefits.

Tip: Sequential ObjectId

MongoDB driver generates ObjectIds in insertion order. Sorting by _id approximates chronological order.

Gotcha: ObjectId Uniqueness

The 5-byte random value is process-specific. Two processes on the same machine could theoretically collide (extremely rare).

Tip: Embed or Reference? The 80/20 Rule

If you always access data together, embed it. If you access it independently, reference it. The 16MB document size limit is the hard boundary — stay under 1MB for most documents.

Tip: Index Your Query Patterns, Not All Fields

Creating indexes on every field wastes RAM. Use explain() to find in-memory sorts and collection scans. Index only what your actual queries filter on.

Gotcha: No Transaction Rollback for Index Builds

Building an index on a large collection can take hours. If it fails midway, the partial index is silently discarded. Plan index builds during maintenance windows.

Senior Insight

MongoDB's ObjectId is a 12-byte identifier with embedded timestamp and machine ID. The timestamp component makes ObjectId sortable by creation time, which I use for implicit time-based ordering. However, ObjectId reveals the creation timestamp and machine ID, which is a security concern for some applications (you can calculate when a user was created from their ID). For sensitive applications, I generate UUIDs in the application and use them as _id values.

Source: MongoDB Developer Center (https://www.mongodb.com/developer/), MongoDB Engineering Blog (https://www.mongodb.com/blog/channel/engineering-blog), Studio 3T Blog (https://studio3t.com/blog/)

────────────────────────────────────────────────────────
<-- back to posts