Home > database >  Iterating over mongo cursor gives me empty results
Iterating over mongo cursor gives me empty results

Time:09-10

I'm using the MongoDB API on Python and was trying to iterate over the results of a query, I know that the find() method returns me a cursor and that I can iterate through it like a list in python. the code I was running looks like this:

query_results = mongo_collection.find(query)
emails_dict = {x.get("userId"): x.get("emailAddr") for x in query_results}
send_emails_dict = {x.get("userId"): x.get("sendEmailNotification") for x in query_results}

But for some strange reason both the dictionaries that I'm trying to create are returned as empty, I checked the results of the query and it's not empty, meaning that there are documents in the query_result. Any clue what's going on? Is this a know bug from pymongo?

CodePudding user response:

Try removing for loop and run again

CodePudding user response:

When you run a .find() it returns a cursor which can be iterated.

Once you have exhausted the cursor it won't return any further values. A simple fix in your code (assuming there's not a huge number of records) is to create a list from the cursor:

query_results = list(mongo_collection.find(query))

Then both the queries should work.

Alternatively put the query results in a for loop and create the dicts from the resulting record, rather than using the dict comprehension twice.

  • Related