我有一个在名称字段上具有唯一索引的集合。这是getIndexes()的输出:
{
"v" : 2,
"unique" : true,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "xxx",
"sparse" : true,
"background" : true,
"collation" : {
"locale" : "en",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 2,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
}
}
现在,当我执行.find(name: 'B').explain('executionStats')
时,在输出中看到它执行了集合扫描而不是索引扫描。 explain('executionStats')
的输出:
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 0,
"totalDocsExamined" : 26,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "B"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 28,
"advanced" : 1,
"needTime" : 26,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"direction" : "forward",
"docsExamined" : 26
}
}
我不明白为什么它不使用索引来扫描文档。请注意,docExamined
中的计数26是集合中文档的总数。