我收集了大约2 000 000份文件。
字段unit
具有索引:{"unit" : 1.0}
查询:
db.getCollection('CollectionName').find({"unit":"value"}).count()
执行5秒(结果200 000个文档)
查询:
db.getCollection('CollectionName').find({"unit":"value"})
执行0.005秒
为什么呢?
答案 0 :(得分:3)
find()
返回一个光标,find().count()
耗尽该光标以计算其中的文档数。
您可能更喜欢使用db.collection.count,例如:
db.getCollection('CollectionName').count({"unit":"value"})