Your MongoDB Version : 4.0
To start with the default count method, cursor count is getting deprecated since 4.0 due to some consistency issues.
Now the issues, the new methods for the count are broken into two parts
- estimatedDocumentCount()
- countDocuments()
The first one is used to get count of documents for the collection, it directly gives you the data from the metadata of the collection hence can be slightly delayed if your collection has a lot of writes taking place on it.
The second one entertains the query filter part but under the hood works using this method:
db.collection.aggregate([
{ $match: <query> },
{ $group: { _id: null, n: { $sum: 1 } } }
])
The problem with this approach is for the DBMS to get you the count it has to manually count those documents, for which it will have to keep those documents in the WiredTiger Internal Cache, and hence on a big collection it would be slow and problematic, directly proportional to size of the collection.
For such scenarios try not repeating the count, use computed pattern discussed here.
There is a good possibility that in the current query you are running it is doing a manual scan and is struggling in getting all the data in WT Cache if it wasn't already there. For starters if you have to use query predicate and count then make sure there is an index over the search term but my vote, if possible, will always go to using computed pattern.