I'm looking to upgrade from Pymongo 3.7 to 3.11 and ran into problems replacing the deprecated function cursor.count(). I need to know the number of documents returned from a query.

collection.count_documents({query}) is not the same as cursor.count(). First it's called on a collection, not a cursor. Second it the function parameters are different.
-Speedwise this function is acceptable.
-But in my current application layer there is no knowledge of the query. Hence, there is no knowing the argument to pass into cursor.collection.count_documents()

collection.estimated_document_count() gives the total of documents in a collection, not the amount of documents in the returned query.

len(list(cursor)) is too slow for my needs.


Is there another way of counting the number of documents returned from a query?
count_documents is implemented via the driver constructing an aggregation pipeline and using the $count stage. There isn't a way to "attach" this count to a cursor, or to a find query.
You might need to rearchitect your application to pass the conditions to count_documents.
Q: Is there an equivalent replacement to Pymongo `cursor.count()`? (3.7 -> 3.11)