On this page本页内容
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.默认情况下,MongoDB在创建集合期间在_id
字段上创建唯一索引。
New Internal Format新的内部格式
Starting in MongoDB 4.2, for featureCompatibilityVersion (fCV) of 4.2 (or greater), MongoDB uses a new internal format for unique indexes that is incompatible with earlier MongoDB versions. 从MongoDB 4.2开始,对于4.2(或更高版本)的featureCompatibilityVersion(fCV),MongoDB为唯一索引使用了一种新的内部格式,该格式与早期的MongoDB版本不兼容。The new format applies to both existing unique indexes as well as newly created/rebuilt unique indexes.新格式既适用于现有的唯一索引,也适用于新创建/重建的唯一索引。
To create a unique index, use the 要创建唯一索引,请使用db.collection.createIndex()
method with the unique
option set to true
.db.collection.createIndex()
方法并且将unique
选项设置为true
。
For example, to create a unique index on the 例如,要在user_id
field of the members
collection, use the following operation in the mongo
shell:members
集合的user_id
字段上创建唯一索引,请在mongo
shell中使用以下操作:
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.如果对复合索引使用唯一约束,那么MongoDB将对索引键值的组合强制唯一性。
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:members
集合的groupNumber
、lastname
和firstname
字段上创建唯一索引,请在mongo
shell中使用以下操作:
The created index enforces uniqueness for the combination of 创建的索引强制groupNumber
, lastname
, and firstname
values.groupNumber
、lastname
和firstname
值的组合具有唯一性。
For another example, consider a collection with the following document:再举一个例子,考虑一个带有以下文档的集合:
Create a unique compound multikey index on 在a.loc
and a.qty
:a.loc
和a.qty
上创建唯一的复合多键索引:
The unique index permits the insertion of the following documents into the collection since the index enforces uniqueness for the combination of 唯一索引允许将以下文档插入到集合中,因为该索引强制a.loc
and a.qty
values:a.loc
和a.qty
值组合的唯一性:
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.如果集合已包含违反索引唯一约束的数据,MongoDB无法在指定的索引字段上创建唯一索引。
You may not specify a unique constraint on a hashed index.不能在散列索引上指定唯一约束。
For replica sets and sharded clusters, using a rolling procedure to create a unique index requires that you stop all writes to the collection during the procedure. 对于副本集和分片集群,使用滚动过程创建唯一索引需要在该过程中停止对集合的所有写入。If you cannot stop all writes to the collection during the procedure, do not use the rolling procedure. 如果在此过程中无法停止对集合的所有写入,请不要使用滚动过程。Instead, build your unique index on the collection by:相反,可以通过以下方式在集合上建立唯一索引:
db.collection.createIndex()
on the primary for a replica set, ordb.collection.createIndex()
,或db.collection.createIndex()
on the mongos
for a sharded cluster.mongos
上发出db.collection.createIndex()
。The unique constraint applies to separate documents in the collection. 唯一约束适用于集合中的单独文档。That is, the unique index prevents separate documents from having the same value for the indexed key.也就是说,唯一索引防止单独的文档对索引键具有相同的值。
Because the constraint applies to separate documents, for a unique multikey index, a document may have array elements that result in repeating index key values as long as the index key values for that document do not duplicate those of another document. 由于该约束适用于单独的文档,因此对于唯一的多键索引,只要该文档的索引键值不与另一个文档的索引键值重复,该文档可能具有导致重复索引键值的数组元素。In this case, the repeated index entry is inserted into the index only once.在这种情况下,重复的索引项只插入索引一次。
For example, consider a collection with the following documents:例如,考虑一个具有以下文档的集合:
Create a unique compound multikey index on 在a.loc
and a.qty
:a.loc
和a.qty
上创建唯一的复合多键索引:
The unique index permits the insertion of the following document into the collection if no other document in the collection has an index key value of 如果集合中没有其他文档的索引键值为{ "a.loc": "B", "a.qty": null }
.{ "a.loc": "B", "a.qty": null }
,则唯一索引允许将以下文档插入集合。
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. 由于唯一约束,MongoDB只允许一个缺少索引字段的文档。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
:x
上有一个唯一的索引:
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
:x
的文档,则唯一索引允许插入不带字段x
的文档:
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
:x
的文档,则插入不带字段x
的文档时,唯一索引发生错误:
The operation fails to insert the document because of the violation of the unique constraint on the value of the field 由于违反了字段x
:x
值的唯一约束,操作无法插入文档:
See also参阅
New in version 3.2.版本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.partialFilterExpression
和唯一约束,则唯一约束仅适用于满足筛选表达式的文档。
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.有关示例,请参阅具有唯一约束的部分索引。
You cannot specify a unique constraint on a hashed index.不能在哈希索引上指定唯一约束。
For a ranged sharded collection, only the following indexes can be unique:对于范围切分的集合,只有以下索引可以是唯一的:
_id
index; however, the _id
index only enforces the uniqueness constraint per shard if the _id
field is not the shard key or the prefix of the shard key._id
索引;但是,如果_id
字段不是分片键或分片键的前缀,则_id
索引仅对每个分片强制唯一性约束。
Uniqueness and the 唯一性和_id
Index_id
索引
If the 如果_id
field is not the shard key or the prefix of the shard key, _id
index only enforces the uniqueness constraint per shard and not across shards._id
字段不是分片键或分片键的前缀,则_id
索引仅对每个分片强制唯一性约束,而不是跨多个分片强制唯一性约束。
For example, consider a sharded collection (with shard key 例如,考虑一个跨越两个碎片A和B的锐化集合(具有分片键{x: 1}
) that spans two shards A and B. {x:1 }
)。Because the 因为_id
key is not part of the shard key, the collection could have a document with _id
value 1
in shard A and another document with _id
value 1
in shard B._id
键不是分片键的一部分,集合可以在分片A中具有一个具有_id
值1
的文档,并且在分片B中也有一个具有_id
值1
的文档。
If the 如果_id
field is not the shard key nor the prefix of the shard key, MongoDB expects applications to enforce the uniqueness of the _id
values across the shards._id
字段既不是分片键,也不是分片键的前缀,MongoDB希望应用程序在各个分片上强制执行_id
值的唯一性。
The unique index constraints mean that:独特的索引约束意味着: