Mongodb中可以使用ensureIndex/createIndex+unique:true来创建unique index。
The unique constraint on indexes ensures that only one document can have a value for a field in acollection. For sharded collections these unique indexes cannot enforce uniqueness because insert and indexing operations are local to each shard.
MongoDB does not support creating new unique indexes in sharded collections and will not allow you to shard collections with unique indexes on fields other than the _id field.
对于unique index理解起来很简单 就是为了让field保持唯一,但需要注意对于sharded collections而言无法对_id意外的field做unique index。
Comment