mongodb count vs find with count [duplicate]

左心房为你撑大大i 提交于 2019-12-10 11:55:13

问题


I am performing a count of documents in a mongo (versions 2.4 and 3.2) collection. The collection is very big, 3821085 documents. I need to count all documents with a reference _id. I have tried two different queries:

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

This query takes a very long time. So much time that I have not let it complete, more than 5 minutes and I get scared and kill it.

For this collection field._id is not an index. I do not have relevant info to use an index with this query.

Is there a better approach to count document in mongo.

UPDATE:

I understand that I need an index on the field field._id. If I did have an index for the field which approach would perform better on a large collection db.SampleCollection.find(...).count() or db.SampleCollection.count(...)? Or is there no difference between the two?


回答1:


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/




回答2:


You should add an index on field._id like this:

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

Then all the queries trying to find/count documents by that field will use this index and will perform faster. For example:

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

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



来源:https://stackoverflow.com/questions/40061221/mongodb-count-vs-find-with-count

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!