PyMongo cursor iteration

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({}) #return all
        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

+3
4

, , , , :

col = pymongo.Connection().db.collection
cur = col.find({})

obj = next(cur, None)
if obj:
    age = obj['age']
    gender = obj['gender']

, . , ORM, , : http://mongoengine.org/

+11

- , , . PyMongo haveNext, next, , StopIteration ( Python).

: , , __getattr__, Python.

, - :

class DataIter(object):

    def __init__(self, cursor):
        self._cursor = cursor
        self._doc = None

    def next(self):
        try:
            self._doc = self._cursor.next()
        except StopIteration:
            self._doc = None
        return self

    def __getattr__(self, key):
        try:
            return self._doc[key]
        except KeyError:
            raise AttributeError('document has no attribute %r' % name)
+1

:

class Cursor(object):

    def __init__(self):
        # mongo connection
        self.collection = pymongo.Connection().cursorcollection
        self.loaded = False
        self.cursor = None

    # Cursor calls (for iterating through results)
    def init_cursor(self):
        """ Opens a new cursor """
        if not self.cursor:
            self.cursor = self.collection.find({})

    def get_next(self):
        """ load next object """
        if self.cursor and self.cursor.alive:
            self.set_data(next(self.cursor))
            return True
        else:
            self.cursor = None
            return False

    def has_next(self):
        """ cursor alive? """
        if self.cursor and self.cursor.alive:                                                                                                                                                                                                                                
            return True
        else:
            return False
+1

python,

class DataIter:
    def __init__(self):
         self.collection = pymongo.Connection().db.collection
         self.cursor = self.collection.find({}) #return all
         self.age = None
         self.gender = None
    def __iter__(self):
         return self
    def next(self):
        if self.cursor.hasNext():
            data = self.cursor.next()
            self.set_data(data)
            return self
        else:
            raise StopIteration

:

for c in DataIter():
    age = c.age
    gender = c.gender
0
source

All Articles