mongodb - python mongoengine mapping to the existing collection -
i'm new mongo engine , having bit of trouble understanding how functions - documentation provided not straight forward. have collection in mongo each document has fields. mapped these fields fields in derived document class , referenced collection per alias.
class imported_item(me.document): _id = me.objectidfield(required = true) _type = me.stringfield(max_length=10) _name = me.stringfield(max_length=10) def item_print(self): print ("************************************************") print self._id print self._type print self._name me.meta = { 'db_alias': 'test', 'index_background': true, 'indexes': [(_type, 1),(_name, 1)], } def main(): me.register_connection(alias="test", name=_database, host=_host, port=_port, username=_username, password=_password) print imported_item.objects({imported_item._type:'sm_tags'}) imported_item.item_print()
now trouble i'm getting that: 1) mongoengine complains requires default connection, not 1 called test, not looking have several collections , have several object types linked each collection, can work them in parallel 2) have bunch of objects of mongoengine.fields.stringfield coming out in print, not values 3) if trying fetch objects imported_item.objects.all(), comes empty set.
i use _mysql driver , more procedure-based, hence don't understand @ level linking between db , mongoengine happening , how can access values in db.
thanks in advance!
i'm not sure example code indented correctly however, @ points.
1) need connect database - you've registered 1 haven't connected it. can use get_connection
fetch connection alias.
from mongoengine.connection import get_connection get_connection(alias)
2) print method looks fine. however, needs called on instance of class - not static method - try:
imported_item.objects({imported_item._type:'sm_tags'}).first().item_print()
3) if coming empty set because haven't set meta correctly - should attribute of document class - not trying set me.meta
eg:
meta = { 'db_alias': 'test', 'index_background': true, 'indexes': [(_type, 1),(_name, 1)], }
finally - please pep8 syntax , conforming python coding standards accepted community wide python.
Comments
Post a Comment