简体   繁体   中英

Mongodb query return type

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM