通配符索引限制
在本页面
- 不兼容的索引类型或属性
- 不支持的查询和聚合模式
-
不兼容的索引类型或属性
通配符索引不支持以下索引类型或属性:
注意
不支持的查询和聚合模式
字段不存在
通配符索引是sparse的,不索引空字段。因此通配符索引不支持查询字段不存在的文档。
例如,考虑一个在product_attributes
上具有通配符索引的集合目录。通配符索引不能支持以下查询:
db.inventory.find( {"product_attributes" : { $exists : false } } )
db.inventory.aggregate([
{ $match : { "product_attributes" : { $exists : false } } }
])
字段等于文档或数组
通配符索引为文档或数组的内容生成条目,而不是文档/数组本身。因此通配符索引不能支持精确的文档/数组相等匹配。通配符索引可以支持查询字段等于空文档{}的位置。
例如,考虑一个在 product_attributes 上具有通配符索引的集合目录。通配符索引不能支持以下查询:
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" ] } }
}])
字段不等于文档或数组
通配符索引为文档或数组的内容生成条目,而不是文档/数组本身。因此通配符索引不能支持精确的文档/数组不等匹配。
例如,考虑一个在product_attributes
上具有通配符索引的集合目录。通配符索引不能支持以下查询:
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" ] ] }
}])
字段不等于null
如果给定字段是集合中任何文档中的数组,通配符索引不能支持查询该字段不等于null的文档。
例如,考虑一个在product_attributes
上具有通配符索引的集合目录。如果product_attributes
通配符索引不能支持以下查询。标签是集合中任意文档的数组:
db.inventory.find( { $ne : [ "product_attributes.tags", null ] } )
db.inventory.aggregate([{
$match : { $ne : [ "product_attributes.tags", null ] }
}])
分片
您不能使用通配符索引来分片集合。在要分片的一个或多个字段上创建一个非通配符索引。有关分片键选择的更多信息,请参见分片 键。
参见