Interpretation of the cursor returned by pymongo client

Hello everyone,

I’m trying to follow the your_frst_read.ipynb notebook. I have a confusion regarding the cursor returned by the find method. Basically I have two scenarios of usage.

Note: Instead of the already present bson.json_utils.dumps method to display the result, I’m trying to use the pprint.pprint function.
First,

cursor = movies.find( { "cast": "Salma Hayek" } )
pprint(list(cursor))

which returns an empty list to the output. Even trying to display with build-in print function spits an empty list.

Second,

pprint(list(movies.find( { "cast": "Salma Hayek" } )))

which prints the actual result.

My question here is, shouldn’t these two scenarios give same ouputs? I’m confused on what’s going on here?

Never mind, I’m dumb. I was trying to iterate through the cursor multiple times, whereas the cursor returned by the mongo client can only be iterated once. If I re-run the cursor = movies.find(...) before doing pprint(list(cursor)), it prints the desired output.

I probably spend more than 3 hours trying and searching, I hope this post can save somebody the time I probably wasted.

The time was not wasted if you figured it out all by yourself!

2 Likes

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