I have a collection with tens of millions of documents.
When performing a count()
I see that MongoDB parses this as an aggregation query with an empty $match
and a $group
with {_id: 1, $sum: 1}
.
It takes an unignorable amount of time for the query to perform.
My question is why doesn't MongoDB optimize this query?
It's a matter of reading a metadata variable of the collection and returning it (that is, the total number of documents).
Thanks!