嵌入文档上的MongoDB索引边界

嵌入文档上的MongoDB索引边界,mongodb,indexing,Mongodb,Indexing,我有一个场景,其中我的“_id”字段包含一个嵌入文档,我看到比较运算符($gte/$lte)在用于嵌入文档中的字段时有一种奇怪的行为 例如,考虑下面的集合,它有9个文档,每个文档都有一个嵌入文档,作为“yID” db.DocumentWithCompoundKeyCollection.find() 现在,当我运行一个查询以返回“Part1”>=1和“Part1”的所有文档时,我从来没有使用对象进行过这种比较。也许MongoDB没有正确处理它 为了找到您想要的范围,您可以尝试: db.Docume

我有一个场景,其中我的“_id”字段包含一个嵌入文档,我看到比较运算符($gte/$lte)在用于嵌入文档中的字段时有一种奇怪的行为

例如,考虑下面的集合,它有9个文档,每个文档都有一个嵌入文档,作为“yID”

db.DocumentWithCompoundKeyCollection.find()


现在,当我运行一个查询以返回“Part1”>=1和“Part1”的所有文档时,我从来没有使用对象进行过这种比较。也许MongoDB没有正确处理它

为了找到您想要的范围,您可以尝试:

db.DocumentWithCompoundKeyCollection.find({ "_id.Part1" : { $gte : 1, $lte : 3 } })

有关
$gte
$lte
的更多信息,请参见我从来没有使用对象进行过这种比较。也许MongoDB没有正确处理它

为了找到您想要的范围,您可以尝试:

db.DocumentWithCompoundKeyCollection.find({ "_id.Part1" : { $gte : 1, $lte : 3 } })

有关
$gte
$lte
的更多信息,请参见

谢谢andresk,但不幸的是,我的代码正在使用另一个依赖于此架构和查询模式的模块(因此我无法更改)。谢谢andresk,但不幸的是,我的代码正在使用另一个依赖于此架构和查询模式的模块(所以我不能改变它)。
"winningPlan" : {
    "stage" : "FETCH",
        "filter" : {
           "$and" : [
                    {
                        "_id" : {
                                "$lte" : {
                                        "Part1" : 3
                                }
                        }
                    },
                    {
                        "_id" : {
                                "$gte" : {
                                        "Part1" : 1
                                }
                        }
                    }
            ]
    },
    "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "_id" : 1
            },
            "indexName" : "_id_",
            "isMultiKey" : false,
            "isUnique" : true,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                    "_id" : [
                            "[{ Part1: 1.0 }, { Part1: 3.0 }]"
                    ]
            }
    }
},
db.DocumentWithCompoundKeyCollection.find({ "_id.Part1" : { $gte : 1, $lte : 3 } })