Finding documents one by one

I want to get the documents with a field set to True. But I don’t want to get all the documents into my program memory and iterate over them. Is there a way I can get those documents one by one?

Hi @Roshan_Dash,
You can use skip(<index>) and limit(1) to achieve your goal.
Good luck,
Rafael,

1 Like

@Roshan_Dash,
I see you marked my answer as a solution.
However, In a second thought the right way to do what you want is to iterate the cursor returned by the find method.
Thanks,
Rafael,

1 Like

Thank you. This seems better and easier to use. I am using pymongo so, I ended up using something like:

data=collection.find()
while True:
try:
print(data.next())
except:
break

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