mongodb.countDocuments
is slow when result set is large
Test data on users collection:
\'active\'
Counting seems like one of those things that should be cheap, but often isn't. Because mongo doesn't maintain a count of the number of documents that match certain criteria in its b-tree index, it needs to scan through the index counting documents as it goes. That means that counting 100x the documents will take 100x the time, and this is roughly what we see here -- 0.018 * 100 = 1.8s
.
To speed this up, you have a few options:
estimatedDocumentCount() - db.users.countDocuments({status: 'inactive'})
. Would this be accurate enough for your use case?counts
document in a separate collection that you keep in sync with the number of active/inactive documents that you have.