Skip to Main Content

MongoByte MongoDB Logo

Welcome to the new MongoDB Feedback Portal!

{Improvement: "Your idea"}
We’ve upgraded our system to better capture and act on your feedback.
Your feedback is meaningful and helps us build better products.

Status Submitted
Categories Database
Created by Guest
Created on Apr 7, 2021

provide faster way to perform a count() on million documents

a couple of customer a shifting from MongoDB to other DBs like Redis or even PostreSQL as count is too slow on MongoDB when there are a lot of document to count even with use of indexes
  • Attach files
  • Guest
    Feb 14, 2025
    To build on this, we have really bad performance doing a `countDocuments()` call on a covered index. It takes >5s on a collection with: ``` totalKeysExamined: 769298, totalDocsExamined: 0, ``` I get that under the hood, the countDocuments() needs to scan the index, so will have O(n) time. Since we want to call this count frequently (ideally every minute), it would be nice if this wasn't so bad. An acceptable workaround for us would be building a partial index with a filter that matches our countDocuments() filter, and then it would be really nice if we could get a estimatedCountDocuments() from the metadata of the index size, which is currently unavailable at the index level
  • Guest
    Nov 11, 2024
    More specifically, it is better having meta on index to get the key below to the index page. So you don't have to scan the key, but return the meta count.