0

I'm using MLab to host a MongoDB database for a node.js project I'm working on. I want to use the uids automatically given to document to identify specific documents across collections. I was wondering if the default ids are unique only in the collection the document is in, or if they are unique throughout the entire database.

{
    "_id": { // <<<< this is what I'm talking about
        "$oid": "5c0754d8d0d1c81b400690a3"
    },
    "owned": [],
    "equipped": [],
    "prospects": [],
    "username": "Urist",
    "password": "dcddb75469b4b4875094e14561e573d8",
    "date": {
        "$date": "2018-12-05T04:32:24.690Z"
    },
    "trades": [],
    "__v": 0
}
Phineas
  • 35
  • 8
  • https://stackoverflow.com/questions/4677237/possibility-of-duplicate-mongo-objectids-being-generated-in-two-different-colle – numbers1311407 Dec 05 '18 at 21:19

1 Answers1

1

_id must be unique across a single collection. It is not necessary for _id to be unique for documents in different collection or database.

In https://docs.mongodb.com/manual/core/document/#field-names:

The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array.

kevinadi
  • 13,365
  • 3
  • 33
  • 49