Pavel M.
Pavel M.

Reputation: 610

Read MongoEngine DynamicDocuments

my issue is that I am saving dict objects with MongoEngine:

class MongoRecord(DynamicDocument):
    record_id = SequenceField(primary_key = True)

class SimpleMongo(object):
    def __init__(self, *args, **kwargs):
        """
        Very simple dict-like Mongo interface
        """
        if PY_VERSION == 2:
            self.iterattr = 'iteritems'
        else:
            self.iterattr = 'items'
        self.debug = DEBUG
        self.dict_type = type(dict())
        self.dbname = kwargs.get('dbname', 'untitled')
        self.collection_name = kwargs.get('collection', 'default')
        self.ip = kwargs.get('ip', '127.0.0.1')
        self.port = kwargs.get('port', 27017)
        self.dbconn = connect(self.dbname, host=self.ip, port=self.port)
        drop = kwargs.get('drop', False)
        if drop:
            self.dbconn.drop_database(self.dbname)

    def put(self, data):
        """
        Put dict
        """
        assert type(data) == self.dict_type
        record = MongoRecord()
        record.switch_collection(self.collection_name)
        generator = getattr(data, self.iterattr)
        __res__ = [setattr(record, k, v) for k,v in generator()] # iteritems() for Python 2.x
        record.save()

but when trying to access them:

def get(self):
    record = MongoRecord()
    record.switch_collection(self.collection_name)
    return record.objects

getting

mongoengine.queryset.manager.QuerySetManager object, not an iterator. So, what is the proper way to get my data back from Mongo being saved as DynamicDocument?

Upvotes: 0

Views: 400

Answers (1)

malla
malla

Reputation: 1708

The problem isn't that MongoRecordis a DynamicDocument or that it contains a dict. You would get the same result with a regular Document. Your problem is with querying, you should change record.objects to MongoRecord.objects to get a cursor.

Regarding your usage of switch_collection()... If MongoRecord documents will be saved to a collection with the same name, at most times, you can define this like below, and you don't have to use switch_collection() when a collection with that name is being queried.

class MongoRecord(DynamicDocument):
    record_id = SequenceField(primary_key = True)
    meta = {'collection': 'records'}

In case you do want to retrieve MongoRecord documents from a collection which isn't called 'records', and you want to define a function for this (which can give an UnboundLocalError), you can do it like this (source):

from mongoengine.queryset import QuerySet


def get(self):
    new_group = MongoRecord.switch_collection(MongoRecord(), self.collection_name)
    new_objects = QuerySet(MongoRecord, new_group._get_collection())
    all = new_objects.all()
    # If you would like to filter on an MongoRecord attribute: 
    filtered = new_objects.filter(record_id=1)
    return all

Upvotes: 2

Related Questions