MongoDB 文档 ID 是对整个数据库还是对那个集合唯一的?
Are MongoDB document IDs unique to the entire database or just that collection?
我正在使用 MLab 为我正在处理的 node.js 项目托管 MongoDB 数据库。我想使用自动提供给文档的 uid 来识别集合中的特定文档。我想知道默认 ID 是否仅在文档所在的集合中是唯一的,或者它们是否在整个数据库中都是唯一的。
{
"_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
}
_id
在单个集合中必须是唯一的。对于不同集合或数据库中的文档,_id
不必是唯一的。
在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.
我正在使用 MLab 为我正在处理的 node.js 项目托管 MongoDB 数据库。我想使用自动提供给文档的 uid 来识别集合中的特定文档。我想知道默认 ID 是否仅在文档所在的集合中是唯一的,或者它们是否在整个数据库中都是唯一的。
{
"_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
}
_id
在单个集合中必须是唯一的。对于不同集合或数据库中的文档,_id
不必是唯一的。
在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.