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.
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 typeObjectId
.(1) According to MongoDB docs the ObjectId is generated by clients:
(2) So, how to ensure your ObjectId is unique?
See this MongoDB blog article Generating Globally Unique Identifiers for Use with MongoDB topics: