私の "_id"フィールドに埋め込みドキュメントが含まれていて、埋め込みドキュメント内のフィールドに対して比較演算子($ gte/$ lte)を使用すると、好奇心を抱いています。埋め込みドキュメントのMongoDBインデックスの境界
今、私はすべてのドキュメントを返すようにクエリを実行すると、「_id」など9つの文書を持って集まり、埋め込まれた文書と、各以下
db.DocumentWithCompoundKeyCollection.find()
{ "_id" : { "Part1" : 1, "Part2" : 1 }, "SomeValue" : BinData(3,"B8+yWvTV4kS/u3e6Cv8Kcw==") }
{ "_id" : { "Part1" : 1, "Part2" : 2 }, "SomeValue" : BinData(3,"eLS1ONAoGUawW+v+vQdFDQ==") }
{ "_id" : { "Part1" : 1, "Part2" : 3 }, "SomeValue" : BinData(3,"m7WsIyInIEmsgWUMcsJPAw==") }
{ "_id" : { "Part1" : 2, "Part2" : 4 }, "SomeValue" : BinData(3,"z7/2j0g4AUikqS5K1TzZig==") }
{ "_id" : { "Part1" : 2, "Part2" : 5 }, "SomeValue" : BinData(3,"WudfqGYE8U+YwWe3Q0qL1w==") }
{ "_id" : { "Part1" : 2, "Part2" : 6 }, "SomeValue" : BinData(3,"B60SpSmXdUGn6AJDu1JIzg==") }
{ "_id" : { "Part1" : 3, "Part2" : 7 }, "SomeValue" : BinData(3,"xVmhanYiV0+dOdTx7PAZkw==") }
{ "_id" : { "Part1" : 3, "Part2" : 8 }, "SomeValue" : BinData(3,"5NNdVzErt0qephmCMRR1nQ==") }
{ "_id" : { "Part1" : 3, "Part2" : 9 }, "SomeValue" : BinData(3,"mhTiJoHGKkCPUeglCfLUoQ==") }
考えてみましょう"Part1"> = 1と "Part1" < = 3、私はすべての9つのドキュメントを取得する必要がありますが、mongoは6つのドキュメント({Part1 ":3 ...}を含むすべてのドキュメントをスキップします)
db.DocumentWithCompoundKeyCollection.find({ "_id":{ "$ GTE":{ "その1":1}、 "$のLTE":{ "その1":3}}})
{ "_id" : { "Part1" : 1, "Part2" : 1 }, "SomeValue" : BinData(3,"B8+yWvTV4kS/u3e6Cv8Kcw==") }
{ "_id" : { "Part1" : 1, "Part2" : 2 }, "SomeValue" : BinData(3,"eLS1ONAoGUawW+v+vQdFDQ==") }
{ "_id" : { "Part1" : 1, "Part2" : 3 }, "SomeValue" : BinData(3,"m7WsIyInIEmsgWUMcsJPAw==") }
{ "_id" : { "Part1" : 2, "Part2" : 4 }, "SomeValue" : BinData(3,"z7/2j0g4AUikqS5K1TzZig==") }
{ "_id" : { "Part1" : 2, "Part2" : 5 }, "SomeValue" : BinData(3,"WudfqGYE8U+YwWe3Q0qL1w==") }
{ "_id" : { "Part1" : 2, "Part2" : 6 }, "SomeValue" : BinData(3,"B60SpSmXdUGn6AJDu1JIzg==") }
.explain()を追加すると正しいindexBoundsが返されるので、最後の3つのドキュメントが返されないのはなぜですか?
インデックス計画
"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 }]"
]
}
}
},
おかげでandresk見つけることができますが、残念ながら私のコードが依存している他のモジュールを使用していますこのスキーマとクエリパターンについては(私はそれを変更することはできません)。 –