Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17415

Where is the ObjectID `_id` generated?

When inserting a document into a MongoDB and no _id is assigned explicitly, one is assigned automatically. My question is, where is this assignment made and where is the ID generated? Is it generated by the client, before sending the insert request, or is it on the server side?

The context of my question is that I want to use MongoDB to build an "event store" (in the sense of "event sourcing"). Part of that is that the store enforces an ordering on the events. There is already an internal ordering in MongoDB, which is sufficient. However, at some point I may have to resume reading events at some point. For that, I can't use the last processed ID in the filter expression, because the process-unique and random parts of the ID don't guarantee any ordering. However, the timestamp part of the ID could do that, if only it was guaranteed to rise monotonically.

If the ID is generated by the server (and that server doesn't do anything funny like having a backward jumping system clock), then there is only one place that generates these IDs. Differences in system clocks and latency between different systems becomes irrelevant. Subsequently, I can rely on the timestamp part of the ID increasing monotonically.

Upvotes: 1

Views: 452

Answers (1)

prasad_
prasad_

Reputation: 14287

All clients (e.g., Mongo Shell, a Python, Java or NodeJS application) connects to the MongoDB database server via a driver software. If a value is not supplied by the application to the _id field the driver generates one. (NOTE: I believe, not sure, if the driver fails to assign one, the database server assigns the value for the _id field). The default _id field value is of BSON type ObjectId.


(1) According to MongoDB docs the ObjectId is generated by clients:

IMPORTANT

While ObjectId values should increase over time, they are not necessarily monotonic. This is because they:

  • Only contain one second of temporal resolution, so ObjectId values created within the same second do not have a guaranteed ordering, and
  • Are generated by clients, which may have differing system clocks.


(2) So, how to ensure your ObjectId is unique?

See this MongoDB blog article Generating Globally Unique Identifiers for Use with MongoDB topics:

  • Ensure identifier uniqueness at the database level
  • Use ObjectID as a unique identifier

Upvotes: 3

Related Questions