Home

objectid

ObjectId is a 12-byte identifier used by MongoDB as the default value for the _id field of documents. It is typically represented as a 24-character hexadecimal string and is designed to be unique within a deployment while also encoding creation time information.

The 12 bytes are arranged in three parts: a 4-byte Unix timestamp (in seconds since the epoch),

ObjectIds can be generated on the client side or by the server, allowing documents to be created

In MongoDB, the _id field is indexed by default, making ObjectId an efficient and unique primary key

a
5-byte
value
that
is
generally
tied
to
the
host
and
process
to
help
distinguish
different
instances,
and
a
3-byte
counter
that
increments
with
each
new
ObjectId
generated
in
the
same
context.
The
timestamp
makes
ObjectIds
roughly
sortable
by
creation
time,
while
the
random
value
and
counter
ensure
uniqueness
across
machines,
processes,
and
moments
of
generation.
without
an
immediate
database
round
trip.
The
3-byte
counter
in
each
process
increments
for
every
new
ObjectId
within
the
same
second
and
wraps
around
after
reaching
its
maximum.
The
5-byte
value
varies
between
hosts
or
processes
to
further
reduce
the
chance
of
collision.
for
documents.
Many
language
drivers
expose
utilities
to
convert
between
the
ObjectId
and
its
hexadecimal
representation,
and
to
extract
the
embedded
timestamp
(for
example,
toDate
or
getTimestamp
methods).
ObjectId
is
part
of
BSON,
and
while
it
provides
useful
ordering
and
uniqueness
properties,
the
embedded
timestamp
means
creation
times
can
be
inferred
from
the
value.