mongodbmongodb-c

will bson_oid_init (&oid, NULL) generate the same two _id?


Now, I am using mongodb c driver to insert documents to mongodb. Each time, I used the following codes to generate an _id.

bson_oid_init (&oid, NULL);
 BSON_APPEND_OID (doc, "_id", &oid);

If multiple applications run the above codes at the same time, will bson_oid_init (&oid, NULL) generate the same two _id with some probability?


Solution

  • With some probability > 0, but very, very close to 0. See the ObjectId docs for full details of the structure of an ObjectId, but basically you would need two different processes with the same process id to create a document in the same second on machines with the same MAC address and, finally, have them get the same random-seeded 3-digit counter value.