Wildcard Index Restrictions通配符索引限制

On this page本页内容

Incompatible Index Types or Properties不兼容的索引类型或属性

Wildcard indexes do not support the following index types or properties:通配符索引不支持以下索引类型或属性:

Note

Wildcard Indexes are distinct from and incompatible with Wildcard Text Indexes. 通配符索引不同于通配符文本索引,并且与通配符文本索引不兼容。Wildcard indexes cannot support queries using the $text operator.通配符索引无法支持使用$text运算符的查询。

Unsupported Query and Aggregation Patterns不支持的查询和聚合模式

Field does not exist字段不存在

Wildcard indexes are sparse and do not index empty fields. 通配符索引是稀疏的,不为空字段编制索引。Wildcard indexes therefore cannot support querying for documents where a field does not exist.因此,通配符索引不支持查询不存在字段的文档。

For example, consider a collection inventory with a wildcard index on product_attributes. 例如,考虑在product_attributes上使用通配符索引的集合inventoryThe wildcard index cannot support the following queries:通配符索引无法支持以下查询:

db.inventory.find( {"product_attributes" : { $exists : false } } )

db.inventory.aggregate([
  { $match : { "product_attributes" : { $exists : false } } }
])
Field is equal to a document or an array字段等于文档或数组

Wildcard indexes generate entries for the contents of a document or array, and not the document/array itself. 通配符索引为文档或数组的内容生成条目,而不是文档/数组本身。Wildcard indexes therefore cannot support exact document/array equality matches. 因此,通配符索引无法支持精确的文档/数组相等匹配。Wildcard indexes can support querying where the field equals an empty document {}.通配符索引可以支持字段等于空文档{}的查询。

For example, consider a collection inventory with a wildcard index on product_attributes. 例如,考虑在product_attributes上使用通配符索引的集合inventoryThe wildcard index cannot support the following queries:通配符索引无法支持以下查询:

db.inventory.find({ "product_attributes" : { "price" : 29.99 } } )
db.inventory.find({ "product_attributes.tags" : [ "waterproof", "fireproof" ] } )

db.inventory.aggregate([{
  $match : { "product_attributes" : { "price" : 29.99 } }
}])

db.inventory.aggregate([{
  $match : { "product_attributes.tags" : ["waterproof", "fireproof" ] } }
}])
Field is not equal to a document or array字段不等于文档或数组

Wildcard indexes generate entries for the contents of a document or array, and not the document/array itself. 通配符索引为文档或数组的内容生成条目,而不是文档/数组本身。Wildcard indexes therefore cannot support exact document/array inequality matches.因此,通配符索引无法支持精确的文档/数组不平等匹配。

For example, consider a collection inventory with a wildcard index on product_attributes. 例如,考虑在product_attributes上使用通配符索引的集合inventoryThe wildcard index cannot support the following queries:通配符索引无法支持以下查询:

db.inventory.find( { $ne : [ "product_attributes", { "price" : 29.99 } ] } )
db.inventory.find( { $ne : [ "product_attributes.tags",  [ "waterproof", "fireproof" ] ] } )

db.inventory.aggregate([{
  $match : { $ne : [ "product_attributes", { "price" : 29.99 } ] }
}])

db.inventory.aggregate([{
  $match : { $ne : [ "product_attributes.tags", [ "waterproof", "fireproof" ] ] }
}])
Field is not equal to null字段不等于null

If a given field is an array in any document in the collection, wildcard indexes cannot support queries for documents where that field is not equal to null.如果给定字段是集合中任何文档中的数组,则通配符索引无法支持对该字段不等于null的文档的查询。

For example, consider a collection inventory with a wildcard index on product_attributes. 例如,考虑在product_attributes上使用通配符索引的集合inventoryThe wildcard index cannot support the following queries if product_attributes.tags is an array in any document in the collection:如果product_attributes.tags是集合中任何文档中的数组,则通配符索引无法支持以下查询:

db.inventory.find( { $ne : [ "product_attributes.tags", null ] } )

db.inventory.aggregate([{
  $match : { $ne : [ "product_attributes.tags", null ] }
}])

Sharding分片

You cannot shard a collection using a wildcard index. 不能使用通配符索引分割集合。Create a non-wildcard index on the field or fields you want to shard on. 在要切分的一个或多个字段上创建非通配符索引。For more information on shard key selection, see Shard Keys.有关碎片关键点选择的详细信息,请参阅碎片关键点