翻译或纠错本页面

唯一索引

A unique index ensures that the indexed fields do not store duplicate values; i.e. enforces uniqueness for the indexed fields. By default, MongoDB creates a unique index on the _id field during the creation of a collection.

Create a Unique Index

To create a unique index, use the db.collection.createIndex() method with the unique option set to true.

db.collection.createIndex( <key and index type specification>, { unique: true } )

Unique Index on a Single Field

For example, to create a unique index on the user_id field of the members collection, use the following operation in the mongo shell:

db.members.createIndex( { "user_id": 1 }, { unique: true } )

Unique Compound Index

You can also enforce a unique constraint on compound indexes. If you use the unique constraint on a compound index, then MongoDB will enforce uniqueness on the combination of the index key values.

For example, to create a unique index on groupNumber, lastname, and firstname fields of the members collection, use the following operation in the mongo shell:

db.members.createIndex( { groupNumber: 1, lastname: 1, firstname: 1 }, { unique: true } )

The created index enforces uniqueness for the combination of groupNumber, lastname, and firstname values.

特性

限制

MongoDB cannot create a unique index on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index.

You may not specify a unique constraint on a hashed index.

跨文档的唯一性限制

唯一性的限制是针对一个集合中不同文档的。也即,唯一索引可以防止 不同 文档的被索引键上存储相同值,但是它不禁止同一篇文档在被索引键存储的数组里存储的元素或者内嵌文档是相同的值。在同一篇文档存储重复数据的情况下,重复的值只会被存入索引一次。

例如,一个集合有一个唯一索引 a.b

db.collection.createIndex( { "a.b": 1 }, { unique: true } )

假如在集合中没有其他的文档的 a.b 键的值是 5 ,那么唯一索引将会允许将以下文档插入集合:

db.collection.insert( { a: [ { b: 5 }, { b: 5 } ] } )

唯一索引与被索引键缺失(Missing Field)

If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.

For example, a collection has a unique index on x:

db.collection.createIndex( { "x": 1 }, { unique: true } )

The unique index allows the insertion of a document without the field x if the collection does not already contain a document missing the field x:

db.collection.insert( { y: 1 } )

However, the unique index errors on the insertion of a document without the field x if the collection already contains a document missing the field x:

db.collection.insert( { z: 1 } )

The operation fails to insert the document because of the violation of the unique constraint on the value of the field x:

WriteResult({
   "nInserted" : 0,
   "writeError" : {
      "code" : 11000,
      "errmsg" : "E11000 duplicate key error index: test.collection.$a.b_1 dup key: { : null }"
   }
})

Unique Partial Indexes

3.2 新版功能.

Partial indexes only index the documents in a collection that meet a specified filter expression. If you specify both the partialFilterExpression and a unique constraint, the unique constraint only applies to the documents that meet the filter expression.

A partial index with a unique constraint does not prevent the insertion of documents that do not meet the unique constraint if the documents do not meet the filter criteria. For an example, see Partial Index with Unique Constraint.