MongoDB ObjectID uniqueness in a Sharded Cluster

685 views Asked by At

If I have a MongoDB sharded cluster with multiple MongoS instances, will my ObjectID in _id be consistant whichever MongoS I write to?

For example, if I write some data from MongoS#1 and the _id ascends normally, if I then write using MongoS#2 will these _id's also ascend in line with the other writes?

Seeing as part of ObjectID is based on the machine hash and the process id, I can't see that this will so sorting on the ObjectID would be useless. Correct?

Whats the recomendation here?

1

There are 1 answers

4
Sachin Shukla On

How the ObjectId is generated would give you a fair idea about your query.

if you type this in your mongo console,

new ObjectId()

you would see the ObjectId sample value as

ObjectId("558bf61f9f49f303f72dd59b")

Now, lets break and see how mongo generates this. 558bf61f 9f49f3 03f7 2dd59b

  1. Here the first 8 characters (4 bytes) represents the number of seconds (not milliseconds) as timestamp.
  2. the next 3 bytes is the machine identifies.
  3. the next 2 bytes are the process id.
  4. and lastly the 3-bytes are the random value.

http://api.mongodb.org/libbson/current/bson_oid_t.html

So, for sure however mongos you are connected with, as the time goes on it would be in ascending order only (because of the first point) the other points ensure that its always a unique number. Hope it clarifies your doubt.

-$