I am looking to create and handle a cursor in python, how cursors initially work in mongo. I know that the supposed way is to do โresult = collection.find ()โ and do โto write to the resultโ, but I'm looking to wrap the iteration functionality in the class. I would like to be able to create a new class object and call a function, for example. init_cursor () to create a db connection, and find find, which returns the cursor. I would like for me to have a get_next () function that moves to the next result and sets the data members of the class based on the result. Here's the pesudo-code:
class dataIter():
def __init__(self):
self.collection = pymongo.Connection().db.collection
self.cursor = self.collection.find({})
self.age = None
self.gender = None
def get_next(self):
if self.cursor.hasNext():
data = self.cursor.next()
self.set_data(data)
def set_data(self, data):
self.age = data['age']
self.gender = data['gender']
So I could just call:
obj.get_next()
age = obj.age
gender = obj.gender
or some other helper functions for outputting data from each document