通配符索引限制

    在本页面

    通配符索引不支持以下索引类型或属性:

    注意

    通配符索引与通配符文本索引不同,也不兼容。通配符索引不支持使用$text操作符的查询。

    不支持的查询和聚合模式

    字段不存在

    通配符索引是sparse的,不索引空字段。因此通配符索引不支持查询字段不存在的文档。

    例如,考虑一个在product_attributes上具有通配符索引的集合目录。通配符索引不能支持以下查询:

    1. db.inventory.find( {"product_attributes" : { $exists : false } } )
    2. db.inventory.aggregate([
    3. { $match : { "product_attributes" : { $exists : false } } }
    4. ])

    字段等于文档或数组

    通配符索引为文档或数组的内容生成条目,而不是文档/数组本身。因此通配符索引不能支持精确的文档/数组相等匹配。通配符索引可以支持查询字段等于空文档{}的位置。

    例如,考虑一个在 product_attributes 上具有通配符索引的集合目录。通配符索引不能支持以下查询:

    1. db.inventory.find({ "product_attributes" : { "price" : 29.99 } } )
    2. db.inventory.find({ "product_attributes.tags" : [ "waterproof", "fireproof" ] } )
    3. db.inventory.aggregate([{
    4. $match : { "product_attributes" : { "price" : 29.99 } }
    5. }])
    6. db.inventory.aggregate([{
    7. $match : { "product_attributes.tags" : ["waterproof", "fireproof" ] } }
    8. }])

    字段不等于文档或数组

    通配符索引为文档或数组的内容生成条目,而不是文档/数组本身。因此通配符索引不能支持精确的文档/数组不等匹配。

    例如,考虑一个在product_attributes上具有通配符索引的集合目录。通配符索引不能支持以下查询:

    1. db.inventory.find( { $ne : [ "product_attributes", { "price" : 29.99 } ] } )
    2. db.inventory.find( { $ne : [ "product_attributes.tags", [ "waterproof", "fireproof" ] ] } )
    3. db.inventory.aggregate([{
    4. $match : { $ne : [ "product_attributes", { "price" : 29.99 } ] }
    5. }])
    6. db.inventory.aggregate([{
    7. $match : { $ne : [ "product_attributes.tags", [ "waterproof", "fireproof" ] ] }
    8. }])

    字段不等于null

    如果给定字段是集合中任何文档中的数组,通配符索引不能支持查询该字段不等于null的文档。

    例如,考虑一个在product_attributes上具有通配符索引的集合目录。如果product_attributes通配符索引不能支持以下查询。标签是集合中任意文档的数组:

    1. db.inventory.find( { $ne : [ "product_attributes.tags", null ] } )
    2. db.inventory.aggregate([{
    3. $match : { $ne : [ "product_attributes.tags", null ] }
    4. }])

    分片

    您不能使用通配符索引来分片集合。在要分片的一个或多个字段上创建一个非通配符索引。有关分片键选择的更多信息,请参见分片 键

    参见

    原文 - Wildcard Index Restrictions