Skip to main content

What is ObjectId?

ObjectId is a special BSON data type used by MongoDB as a document's unique identifier in the _id field.

The main properties

  1. Uniqueness An ObjectId is generated so that a collision is practically impossible, even across different machines.
  2. A 12-byte structure It consists of:
  • 4 bytes, the creation time (timestamp),
  • 5 bytes, machine and process data,
  • 3 bytes, a counter.
  1. It holds a timestamp An ObjectId lets you tell when a document was created, which is handy for logs and analytics.
  2. Auto-generation MongoDB creates _id: ObjectId(...) automatically if it isn't set by hand.

Summary: an ObjectId is a compact, unique document identifier with an encoded creation time, which MongoDB assigns by default.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.