skip to Main Content

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?

4

Answers


  1. Try removing for loop and run again AS there is no need to add for loop as the function find returns the specific data with that email address you gave as a parameter

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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"]
    
    Login or Signup to reply.
  4. 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}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search