任意场的唯一约束

在本页面

如果您不能使用唯一字段作为分片键,或者需要在多个字段上强制唯一性,则必须创建另一个collection来充当“代理集合”。此集合必须同时包含对原始文档的引用(即其ObjectId)和唯一键。

考虑一个存储用户信息的集合records。字段email不是分片键,但必须是唯一的。

proxy集合将包含以下内容:

{
  "_id" : ObjectId("...")
  "parent_id" : "<ID>"
  "email" : "<string>"
}

使用以下命令在email字段上创建唯一索引:

db.proxy.createIndex( { "email" : 1 }, { unique : true } )

以下示例首先尝试将包含目标字段和生成的唯一 ID 的文档插入proxy集合。如果操作成功,则它将整个文档插入records集合。

records = db.getSiblingDB('records');
proxy = db.getSiblingDB('proxy');

var primary_id = ObjectId();

proxy.insertOne({
   "_id" : primary_id
   "email" : "example@example.net"
})

// if: the above operation returns successfully,
// then continue:

records.insertOne({
   "_id" : primary_id
   "email": "example@example.net"
   // additional information...
})

请注意,此方法需要为primary_id字段创建唯一的 ID,而不是让 MongoDB 在插入文档时自动创建它。

如果您需要在多个字段上强制唯一性,则每个字段都需要自己的代理集合。

See

完整的文档:createIndex()shardCollection

Considerations

首页