According to docs, db.collection.countDocuments()
wraps this:
db.collection.aggregate([
{ $match: <query> },
{ $group: { _id: null, n: { $sum: 1 } } }
])
Even if there is an index, all of the matched docs will be passed into the $group
to be counted, no?
If not, how is mongodb able to count the docs without processing all matching docs?