Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB :: are Mongo IDs unique across collections?

Tags:

mongodb

Can Mongo IDs have the same value in different collections in the same database?

like image 715
CamelCamelCamel Avatar asked Mar 14 '11 20:03

CamelCamelCamel


People also ask

Are Mongo IDs sequential?

Although MongoDB does not support auto-increment sequence as a default feature like some relational databases, we can still achieve this functionality using a counter collection. The counter collection will have a single document that tracks the current unique identifier value.

Are MongoDB IDs predictable?

Mongo ObjectIds are generated in a predictable manner, the 12-byte ObjectId value consists of: a 4-byte value representing the seconds since the Unix epoch, a 3-byte machine identifier, a 2-byte process id, and.

Are Mongoose IDs unique?

yes, the unique: true index guarantees it "in this one collection" - the algorithm "almost" guarantees it universally.

How is MongoDB object ID generated?

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field. The 5 byte "random value" does not appear to be random.


1 Answers

The uniqueness constraint for _id is per collection, so yes - one and the same ID can occur once per Collection.

It's however very unlikely, if not impossible, for the same ID to be generated twice. So in order for this to happen you would have to manually insert duplicate IDs.

like image 125
svjson Avatar answered Oct 01 '22 07:10

svjson