我通过 Casbah(Mongo 的 Scala 库)创建了一个多键复合索引:
db.collection.ensureIndex(MongoDBObject("Header.records.n" -> 1) ++ MongoDBObject("Header.records.v" -> 1) ++ MongoDBObject("Header.records.l" -> 1))
然后,通过 Mongo Shell,我执行了db.collection.find(...).explain
一个nScannedObjects
超出db.collection.count()
. 查看 Mongo文档,似乎需要调用 ensureIndex一次,然后任何写入都会强制更新索引。
但是,我看到了一个帖子,这个帖子只需要调用一次。db.collection.ensureIndex(...)
编辑
>db.collection.find( {"Header.records" : {$all : [
{$elemMatch: {n: "Name", v: "Kevin",
"l" : { "$gt" : 0 , "$lt" : 15}} }]}},
{_id : 1}).explain()
{
"cursor" : "BtreeCursor
Header.records.n_1_Header.records.v_1_Header.records.l_1",
"isMultiKey" : true,
"n" : 4098,
"nscannedObjects" : 9412,
"nscanned" : 9412,
"nscannedObjectsAllPlans" : 9412,
"nscannedAllPlans" : 9412,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 152,
"indexBounds" : {
"Header.records.n" : [
[
"Name",
"Name"
]
],
"Header.records.v" : [
[
"Kevin",
"Kevin"
]
],
"Header.records.l" : [
[
0,
1.7976931348623157e+308
]
]
},
"server" : "ABCD:27017"
请注意,nScanned (9412) > count (4248)。
> db.collection.count()
4248
为什么?