Cursor, count vs count_documents, Deprecation Warning


I’m using pymongo with Python 3.9.4 on Windows 10.

When I do a find and get a Cursor object I call count but that produces this deprecation warning:

DeprecationWarning: count is deprecated. Use Collection.count_documents instead.

The problem there is that deprecation warning is for the Collection class, not the Cursor.

Regardless, I tried just calling count_documents on the object and it cries foul: it’s a Cursor, not a Collection.

res_structs = mongo_coll_structs.find( {‘JobName’ : job_name, ‘ImportStatus’: { ‘$ne’: ‘success’ } }
print("GetStructsForJobs: found this many structs for " + job_name + " : " + str(res_structs.count()))

Here are the packages I have installed:

Package Version

boto3 1.17.51
botocore 1.20.51
jmespath 0.10.0
pip 21.1.1
pymongo 3.11.4
python-dateutil 2.8.1
s3transfer 0.3.7
setuptools 49.2.1
six 1.15.0
urllib3 1.26.4

I found the solution to my problem: I was misinterpreting that deprecation warning.

The warning is instructing me to use Collection.count_documents instead of Cursor.count, it is NOT telling me to use Cursor.count_documents because that doesn’t exist… duh.

The learning curve, heh? :wink: All been there. Welcome to the community @Paul_Hamrick.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.