On this page本页内容
Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value. 稀疏索引仅包含具有索引字段的文档的条目,即使索引字段包含空值也是如此。The index skips over any document that is missing the indexed field. 索引跳过任何缺少索引字段的文档。The index is “sparse” because it does not include all documents of a collection. 索引是“稀疏”的,因为它不包括集合中的所有文档。By contrast, non-sparse indexes contain all documents in a collection, storing null values for those documents that do not contain the indexed field.相比之下,非稀疏索引包含集合中的所有文档,为不包含索引字段的文档存储空值。
Important重要的
Changed in version 3.2.在版本3.2中更改。Starting in MongoDB 3.2, MongoDB provides the option to create partial indexes. 从MongoDB 3.2开始,MongoDB提供了创建部分索引的选项。Partial indexes offer a superset of the functionality of sparse indexes. 部分索引提供了稀疏索引功能的超集。If you are using MongoDB 3.2 or later, partial indexes should be preferred over sparse indexes.如果您使用的是MongoDB 3.2或更高版本,部分索引应该优先于稀疏索引。
To create a 要创建稀疏索引,请使用sparse
index, use the db.collection.createIndex()
method with the sparse
option set to true
. db.collection.createIndex()
方法,并将sparse
选项设置为true
。For example, the following operation in the 例如,mongo
shell creates a sparse index on the xmpp_id
field of the addresses
collection:mongo
shell中的以下操作会在addresses
集合的xmpp_id
字段上创建稀疏索引:
The index does not index documents that do not include the 索引不会索引不包含xmpp_id
field.xmpp_id
字段的文档。
Note
Do not confuse sparse indexes in MongoDB with block-level indexes in other databases. 不要将MongoDB中的稀疏索引与其他数据库中的块级索引混淆。Think of them as dense indexes with a specific filter.可以将它们视为带有特定筛选器的密集索引。
sparse
If a sparse index would result in an incomplete result set for queries and sort operations, MongoDB will not use that index unless a 如果稀疏索引会导致查询和排序操作的结果集不完整,MongoDB将不会使用该索引,除非hint()
explicitly specifies the index.hint()
明确指定该索引。
For example, the query 例如,除非明确提示,否则查询{ x: { $exists: false } }
will not use a sparse index on the x
field unless explicitly hinted. { x: { $exists: false } }
不会在x字段上使用稀疏索引。See Sparse Index On A Collection Cannot Return Complete Results for an example that details the behavior.有关详细行为的示例,请参阅集合上的稀疏索引无法返回完整结果。
Changed in version 3.4.已在3.4版中更改。
If you include a 如果在对集合中的所有文档(即使用空查询谓词)执行hint()
that specifies a sparse index when you perform a count()
of all documents in a collection (i.e. with an empty query predicate), the sparse index is used even if the sparse index results in an incorrect count.count()
时包含一个指定稀疏索引的hint()
,则即使稀疏索引导致不正确的计数,也会使用稀疏索引。
To obtain the correct count, do not 要获得正确的计数,在对集合中的所有文档进行计数时,不要使用稀疏索引hint()
with a sparse index when performing a count of all documents in a collection.hint()
。
sparse
by Defaultsparse
的索引¶2dsphere (version 2), 2d, geoHaystack, and text indexes are always sparse
.2dsphere
(version 2)、2d
、geoHaystack
和text
索引总是稀疏的。
sparse
Sparse compound indexes that only contain ascending/descending index keys will index a document as long as the document contains at least one of the keys.只要文档至少包含一个索引键,只包含升序/降序索引键的稀疏复合索引就会为文档编制索引。
For sparse compound indexes that contain a geospatial key (i.e. 2dsphere, 2d, or geoHaystack index keys) along with ascending/descending index key(s), only the existence of the geospatial field(s) in a document determine whether the index references the document.对于包含地理空间键(即2dsphere
、2d
或geoHaystack
索引键)以及升序/降序索引键的稀疏复合索引,只有文档中地理空间字段的存在才能确定索引是否引用文档。
For sparse compound indexes that contain text index keys along with ascending/descending index keys, only the existence of the 对于包含text索引键和升序/降序索引键的稀疏复合索引,只有text
index field(s) determine whether the index references a document.text
索引字段的存在才能确定索引是否引用文档。
Consider a collection 考虑包含以下文件的集合scores
that contains the following documents:scores
:
The collection has a sparse index on the field 该集合在字段score
:score
上有一个稀疏索引:
Then, the following query on the 然后,以下对scores
collection uses the sparse index to return the documents that have the score
field less than ($lt
) 90
:scores
集合的查询使用score
索引返回分数字段小于($lt
)90
的文档:
Because the document for the userid 由于用户名"newbie"
does not contain the score
field and thus does not meet the query criteria, the query can use the sparse index to return the results:"newbie"
的文档不包含score
字段,因此不符合查询条件,因此查询可以使用稀疏索引返回结果:
Consider a collection 考虑包含以下文件的集合scores
that contains the following documents:scores
:
The collection has a sparse index on the field 该集合在字段得分上有一个稀疏索引:score
:
Because the document for the userid 因为用户ID"newbie"
does not contain the score
field, the sparse index does not contain an entry for that document."newbie"
的文档不包含score
字段,所以稀疏索引不包含该文档的条目。
Consider the following query to return all documents in the 考虑下面的查询,返回scores
collection, sorted by the score
field:scores
字段中的所有文档,由score
字段排序:
Even though the sort is by the indexed field, MongoDB will not select the sparse index to fulfill the query in order to return complete results:即使排序是按索引字段进行的,MongoDB也不会选择稀疏索引来完成查询,以返回完整的结果:
To use the sparse index, explicitly specify the index with 要使用稀疏索引,请使用hint()
:hint()
显式指定索引:
The use of the index results in the return of only those documents with the 使用索引只会返回带有score
field:score
字段的文档:
See also参阅
Consider a collection 考虑包含以下文件的集合scores
that contains the following documents:scores
:
You could create an index with a unique constraint and sparse filter on the 可以使用以下操作在score
field using the following operation:score
字段上创建具有唯一约束和稀疏筛选器的索引:
This index would permit the insertion of documents that had unique values for the 该索引允许插入score
field or did not include a score
field. score
字段具有唯一值或不包含score
字段的文档。As such, given the existing documents in the 因此,考虑到scores
collection, the index permits the following insert operations:score
集合中的现有文档,索引允许以下插入操作:
However, the index would not permit the addition of the following documents since documents already exists with 但是,索引不允许添加以下文件,因为已经存在score
value of 82
and 90
:score
值为82
和90
的文件: