简体   繁体   中英

Iterating over mongo cursor gives me empty results

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?

Try removing for loop and run again

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.

You could try:

query_results = mongo_collection.find(query)

emails_dict = {}
send_emails_dict = {}

for doc in query_results:
    emails_dict |= {doc["userId"]: doc["emailAddr"]}
    send_emails_dict |= {doc["userId"]: doc["sendEmailNotification"]}

Or perhaps more efficiently:

query_results = mongo_collection.find(query)

emails_dict = {}
send_emails_dict = {}

for doc in query_results:
    emails_dict[doc["userId"]] = doc["emailAddr"]
    send_emails_dict[doc["userId"]] = doc["sendEmailNotification"]

Another option is to rewind the cursor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM