[solved] Are _id fields in meteor collections unique?


#1

I’ve just run db.system.indexes.find({"unique": true}); against mongo shell and it didnt show any index on _id field of any collection :cold_sweat:
If they are not - how can I ensure their uniqueness? Eg when I want to have my own _id generation instead of Random's package. Does ensureIndex works with _id field or something?..


#2

_id should be unique. Mongo controls that not Meteor


#3

https://docs.mongodb.org/manual/core/indexes-introduction/#default-id


#4

thx, good to know :smiley:


#5

I thought in Meteor, Meteor controls the _id field? It’s generated via Meteor’s Random.id(), whereas Mongo’s _id is a full object that contains a unique ID as well as a timestamp, and maybe other metadata.


#6

Yes, but Random.id() is still pretty unique. One in 10^30 something…
So basically if every person on earth generated one id per second for a lifetime, the chance of any two of them to be the same is still one in a hundred billion…


#7

It is funny you would say that, because we are hitting non-unique ids on a project today. Where did you find that 10^30 number?


#8

I don’t believe that the Mongo API would allow Meteor to insert duplicate _id’s which is a unique identifier, but someone from MDG or maybe Sacha would likely be better to answer.


#9

Wow, really? What happened, and how did Meteor resolve it? You have the option of making Meteor use Mongo style IDs instead, if it’s an issue.


#10

Random.id() is a 17 digit base 60 number. 60^17 = 1.7e30.

This is of course assuming true randomness, something which is hard, but something I was hoping someone competent who developed Random would have made at least a decent attempt at, given that the science is well known.

If you are hitting duplicate ids using anything short of an insane number of objects, I would say Random is seriously broken.


#11

Yeah, really interesting to know!

  1. does Random generates not-random strings?
  2. does mongo allow non-unique _id’s in collecitons??

#12

Sorry to bump this old thread up but we are currently developing app with Meteor that will collect huge amount of data in a collection (millions) and would like to know if these _id fields generated by Meteor are really unique or should we preferably move to using mongo’s ObjectID format for production? If _id generated by mongo is not checking if there is another document with that _id in the db, what happens when trying to insert something to db and the _id generated by Meteor is not unique, adds duplicate _id or rejects adding it?

Would be great to get answer for this from MDG :slight_smile:


#13

They are random (pseudo-random). On the server, they will be generated using server entropy, but random (or pseudo-random) numbers cannot guarantee uniqeness (for a simplified argument for this, look at the birthday paradox). For guaranteed uniqueness you really need something like an auto-increment.

MongoDB’s internal objectid includes a bunch of stuff including timestamp information, and in most use cases will be better at collision avoidance than a random number. However, Meteor’s implementation of this is still based on a random number.

Rejects. _id has a unique constraint.