Search code examples
mongodbdatabase-performance

Why MongoDB find has same performance as count


I am running tests against my MongoDB and for some reason find has the same performance as count.

Stats: orders collection size: ~20M, orders with product_id 6: ~5K

product_id is indexed for improved performance.

Query: db.orders.find({product_id: 6}) vs db.orders.find({product_id: 6}).count()

result the orders for the product vs 5K after 0.08ms

Why count isn't dramatically faster? it can find the first and last elements position with the product_id index


Solution

  • As Mongo documentation for count states, calling count is same as calling find, but instead of returning the docs, it just counts them. In order to perform this count, it iterates over the cursor. It can't just read the index and determine the number of documents based on first and last value of some ID, especially since you can have index on some other field that's not ID (and Mongo IDs are not auto-incrementing). So basically find and count is the same operation, but instead of getting the documents, it just goes over them and sums their number and return it to you.

    Also, if you want a faster result, you could use estimatedDocumentsCount (docs) which would go straight to collection's metadata. This results in loss of the ability to ask "What number of documents can I expect if I trigger this query?". If you need to find a count of docs for a query in a faster way, then you could use countDocuments (docs) which is a wrapper around an aggregate query. From my knowledge of Mongo, the provided query looks like a fastest way to count query results without calling count. I guess that this should be preferred way regarding performances for counting the docs from now on (since it's introduced in version 4.0.3).