Question

When I make a query in Mongodb using Mongokit in Python, it returns a json document object. However I need to use the return value as a model type that I have defined. For example, if I have the class:

class User(Document):
  structure = {
    'name': basestring
 }

and make the query

 user = db.users.find_one({'name':'Mike'})

I want user to be an object of type User, so that I can embed it into other objects that have fields of type User. However it just returns a json document. Is there a way to cast it or something? This seems like something that should be very intuitive and easy to do.

Was it helpful?

Solution

From what I can see Mongokit is built on the top of pymongo, and pymongo find has an argument called as_class:

as_class (optional): class to use for documents in the query result (default is document_class)

http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top