Skip to content
uencoder

BSON ObjectId

BSON ObjectId is MongoDB's 12-byte default primary key, containing a timestamp, machine identifier, process id, and counter; it is globally unique and roughly ordered.

Purpose

To give MongoDB documents a distributed-safe auto-increment-like primary key that needs no central allocator and is roughly time-ordered, which helps indexes.

Mechanism

12 bytes: a 4-byte timestamp (seconds) plus a 5-byte random machine identifier plus a 3-byte counter. The first 4 bytes make ObjectIds roughly ordered by creation time.

Use cases

  • MongoDB document primary keys
  • Distributed unique ids
  • Roughly ordered ids

Related tools

References