Inserting document with an _id which is already present in different collection

What will happen if I try to insert a document with an _id that is already present in other collection?

In two different collections, there could be documents with the same _id.

The _id is unique in a collection.

The document will be inserted. We can test it:

use testDB
db.coll1.insert({_id:1})
db.coll2.insert({_id:1})
3 Likes

Hi @anujkrz_nath,

The combination of database and collection/view name acts as a namespace in MongoDB and since separate collections will have different namespace so they can have same value for _id field.