mongodb count vs find with count

时间:2016-10-15 16:00:18

标签: mongodb nosql

我正在mongo(版本2.4和3.2)集合中执行文档计数。该集合非常大,3821085文档。我需要使用引用_id计算所有文档。我尝试了两种不同的查询:

db.SampleCollection.find({"field._id" : ObjectId("UUID")}).count() db.SampleCollection.count({"field._id" : ObjectId("UUID")})

此查询需要很长时间。很长一段时间我没有让它完成,超过5分钟,我害怕并杀死它。

对于此集合,field._id不是索引。我没有相关信息来使用此查询的索引。

有没有更好的方法来计算mongo中的文档。

更新

我知道我需要字段field._id上的索引。如果我确实有一个该字段的索引哪个方法在大型集合db.SampleCollection.find(...).count()db.SampleCollection.count(...)上表现更好?或两者之间没有区别?

2 个答案:

答案 0 :(得分:3)

In your scenario, you should have an index.

Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement.

https://docs.mongodb.com/manual/indexes/

UPDATE:

the question asked now is different. Is "collection.find({}).count()" more fast then "collection.count()"?

According to the MongoDB documentation:

count() is equivalent to the db.collection.find(query).count() construct. https://docs.mongodb.com/manual/reference/method/db.collection.count/

答案 1 :(得分:0)

您应该在field._id上添加索引,如下所示:

db.SampleCollection.createIndex( { "field._id": 1 } );

然后,尝试按该字段查找/计算文档的所有查询都将使用此索引,并且执行速度更快。例如:

db.SampleCollection.count({"field._id" : ObjectId("UUID")});

请参阅 - https://docs.mongodb.com/manual/core/index-single/MongoDB 'count()' is very slow. How do we refine/work around with it?