我对Mongodb比较陌生,我无法理解为什么带有$和运算符的查询似乎会执行完整扫描,而没有它的相同查询则没有。
我的文档看起来像这样
{_id:"123456", labels : [{label:"beef", language:"en"}, {...}],...}
我在标签和语言上有一个复合索引,但是,解释显示了非常不同的结果,具体取决于我是否
db.mycollection.find({ "labels" : { "$elemMatch" : { "label" : "beef" , "$and" : [ { "lang" : "en"}]}}}).explain()
{
"cursor" : "Complex Plan",
"n" : 4,
"nscannedObjects" : 0,
"nscanned" : 16701573,
"nscannedObjectsAllPlans" : 0,
"nscannedAllPlans" : 16701573,
"nYields" : 130540,
"nChunkSkips" : 0,
"millis" : 16283,
"filterSet" : false
}
或
db.mycollection.find({ "labels" : {$elemMatch: { label:"beef" ,"lang" : "en" }}}).explain()
{
"cursor" : "BtreeCursor labels.label_1_labels.lang_1",
"isMultiKey" : true,
"n" : 4,
"nscannedObjects" : 4,
"nscanned" : 4,
"nscannedObjectsAllPlans" : 4,
"nscannedAllPlans" : 4,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"labels.label" : [
[
"beef",
"beef"
]
],
"labels.lang" : [
[
"en",
"en"
]
]
},
"filterSet" : false
}
有人可以帮我理解为什么吗?。
提前致谢!