我今天早上的大部分时间都在重新阅读她的 MongoDB文档、博客和其他答案,但我仍然错过了一些我希望对其他人来说非常明显的东西。
编辑:我已将文档的方案更改为没有子文档(元数据。*),并且仍然存在未覆盖索引的问题。我删除了现有索引并用新索引重新索引:
所以不是我有:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.daily"
},
{
"v" : 1,
"key" : {
"host" : 1,
"cid" : 1,
"title" : 1,
"urls" : 1,
"global" : -1,
"current" : -1,
"total" : -1
},
"name" : "byHostTotals",
"ns" : "test.daily"
},
{
"v" : 1,
"key" : {
"host" : 1,
"cid" : 1,
"title" : 1,
"urls" : 1,
"total" : -1,
"global" : -1,
"current" : -1
},
"name" : "byHostCurrents",
"ns" : "test.daily"
}
]
并给出这个查询:
db.daily.find({'host': 'example.com'}, {'_id': 0, 'cid': 1, 'title': 1, 'current': 1}).hint("byHostCurrents").sort({'current': -1}).limit(10).explain()
未显示为被名为“byHostCurrent”的索引所覆盖:
{
"clauses" : [
{
"cursor" : "BtreeCursor byHostCurrents",
"isMultiKey" : true,
"n" : 10,
"nscannedObjects" : 1090,
"nscanned" : 1111,
"scanAndOrder" : true,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"host" : [
[
"example.com",
"example.com"
]
],
"cid" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"title" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"total" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
],
"global" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
],
"current" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
},
{
"cursor" : "BtreeCursor ",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : true,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"host" : [
[
"usatoday.com",
"usatoday.com"
]
],
"cid" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"title" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"total" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
],
"global" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
],
"current" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
}
],
"cursor" : "QueryOptimizerCursor",
"n" : 10,
"nscannedObjects" : 1090,
"nscanned" : 1111,
"nscannedObjectsAllPlans" : 1090,
"nscannedAllPlans" : 1111,
"scanAndOrder" : false,
"nYields" : 8,
"nChunkSkips" : 0,
"millis" : 9,
"server" : "ubuntu:27017",
"filterSet" : false
}
MongoDB版本是:2.6.3。