Convert mongodb return object to dictionary Convert mongodb return object to dictionary python python

Convert mongodb return object to dictionary


What about just using to_mongo method of an object to convert it to a dict?

object.to_mongo()


Expanding on @alexvassel's and @z0r's answers, calling .to_mongo() converts the object to a SON instance. Once you have it, you can call its .to_dict() method to convert it to a dictionary.

For example... (qset is a queryset that's returned from mongoengine, after e.g. Posts.objects.all()).

sons = [ob.to_mongo() for ob in qset]for son in sons:    print str(son.to_dict())