-
Notifications
You must be signed in to change notification settings - Fork 5
Query
namlook edited this page Aug 22, 2011
·
5 revisions
To query a document just use find
, find_one
and get_from_id
:
>>> blogpost = connection.BlogPost.find_one({"title": "Hello World"})
Finding multiple document
>>> blogpost = connection.BlogPost.find()
>>> blogpost.count()
1
>>> for blogpost in blogposts:
... print blogpost['title']
MongoLite implement an helper to fetch a document from its id:
>>> blogpost = connection.BlogPost.get_from_id(blogpost_id)
MongoLite is build on top of pymongo. Connection
, Database
and Collections
all inherit from pymongo's one.
This is great because you don't miss any feature of pymongo and you can access to the pymongo's layer to get raw data.
If you use inheritance, see how you can make better queries
Getting raw data is useful when you only want to have one value from your data. This is fast as there's no wrapping.
To get data as dict, just use MongoLite as you would use pymongo:
>>> doc = connection.mydb.mycol.find_one() # this is a dict
Please see the pymongo's documentation for more details.
See next how to update document