How to check if a pymongo cursor has query results How to check if a pymongo cursor has query results python python

How to check if a pymongo cursor has query results


EDIT: While this was true in 2014, modern versions of pymongo and MongoDB have changed this behaviour. Buyer beware:

.count() is the correct way to find the number of results that are returned in the query. The count() method does not exhaust the iterator for your cursor, so you can safely do a .count() check before iterating over the items in the result set.

Performance of the count method was greatly improved in MongoDB 2.4. The only thing that could slow down your count is if the query has an index set on it, or not. To find out if you have an index on the query, you can do something like

query = collection.find({"string": field})print query.explain()

If you see BasicCursor in the result, you need an index on your string field for this query.


EDIT: as @alvapan pointed out, pymongo deprecated this method in pymongo 3.7+ and now prefers you to use count_documents in a separate query.

item_count = collection.count_documents({"string": field})

The right way to count the number of items you've returned on a query is to check the .retreived counter on the query after you iterate over it, or to enumeratethe query in the first place:

# Using .retrievedquery = collection.find({"string": field})for item in query:    print(item)print('Located {0:,} item(s)'.format(query.retrieved))

Or, another way:

# Using the built-in enumeratequery = collection.find({"string": field})for index, item in enumerate(query):    print(item)print('Located {0:,} item(s)'.format(index+1))


How about just using find_one instead of find ? Then you can just check whether you got a result or None. And if "string" is indexed, you can pass fields = {"string":1, "_id" :0}, and thus make it an index-only query, which is even faster.


Another solution is converting cursor to list, if the cursor doesn't have any data then empty list else list contains all data.

 doc_list = collection.find({}); #find all data have_list = True if len(list(doc_list)) else False;