【发布时间】:2017-12-07 11:04:35
【问题描述】:
我有一个具有重复价值的集合,如下所示
{ "_id" : ObjectId("59f42f500790826aae869517"), "student_id" : 1, "type" : "exam", "score" : 38.09994424551998 }
{ "_id" : ObjectId("59f42f500790826aae869518"), "student_id" : 1, "type" : "homework", "score" : 91.98453327244025 }
{ "_id" : ObjectId("59f42f500790826aae869519"), "student_id" : 1, "type" : "quiz", "score" : 51.50397276737756 }
现在我已将 'student_id' 作为索引,并且有索引与
'student_id,type' , 'student_id,score' & 'student_id,type,score'
。 当我对此进行以下查询时
db.scores.explain("executionStats").find({'student_id':{$gte:10}},{_id:0,type:0,score:0})
我希望应该只使用“student_id”索引,因为所有其他字段都投影为 0,所以我不需要它们。但是它告诉我使用的索引是 'student_id,type'。一个猜测我可以认为mongodb会扫描每个文档以查找特定的student_id以了解文档差异,最好的方法是使用“student_id,type”索引。但是我可能是错的。所以请帮我知道原因。谢谢。
【问题讨论】:
-
创建索引的顺序重要吗?之后我创建了 student_id。如果是这样,我还需要使用 $hint 吗?
标签: mongodb mongodb-query