1

Ok, so I have a members collection and a pet members collection, now i understand the basic workings on how the IDs are generated, but is there a chance that a id that gets assigned to the pets collection could be the same as the members collection?

The reason I ask, is we are improving a social networking site, that uses MongoDB at its core. now while this social site does give each pet an ID ( this is only the case if the owner buys an id ) we use the "_id" or '$id' as the main id which we look for in our database.

So my question is simply is it possible that even knowing they are two totally separate collections could they end up with the same id as each other? - This is what we don't want.

RussellHarrower
  • 6,470
  • 21
  • 102
  • 204

1 Answers1

3

MongoDB's ObjectId is unlikely to collide. It contains a counter, a random number, process id, etc. ObjectIds generated on different servers will have a different random number, process id, etc. ObjectIds generated on the same server will differ in the counter part.

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
  • So what your saying I have 10 members yet I have 3 pet members that is the counter as such? – RussellHarrower Jun 27 '14 at 13:33
  • 2
    MongoDB server and drivers don't know about collections when they generate an id. They just say "hey, give me a new id" and that id will be used for the document in whichever collection it is. The first part of the id is the current time. The counter is used to make ids unique within a second. You can create up to 16,777,216 per second this way. The counter gets reset after a second. – Gergo Erdosi Jun 27 '14 at 13:39