-
Notifications
You must be signed in to change notification settings - Fork 132
Query
There are two ways to query a collection: getting raw data or Document
instance.
Getting raw data is useful when you only want to have one value from your data. This is fast
as there's no validation or wrapping. There is two methods to query raw data : find()
and
find_one()
, one()
and find_random()
.
find()
, and find_one()
act like the similar pymongo's methods.
Please, see the pymongo documentation
one()
act like find()
but will raise a mongokit.MultipleResultsFound
exception if
there is more than one result.
>>> bp2 = tutorial.BlogPost()
>>> bp2['title'] = u'my second blog post'
>>> bp2['author'] = u'you'
>>> bp2.save()
>>> tutorial.one()
Traceback (most recent call last):
...
MultipleResultsFound: 2 results found
>>> tutorial.one({'title':'my first blog post'})
{u'body': None, u'author': u'myself', u'title': u'my first blog post', u'rank': 0, u'_id': ObjectId('4b5ec4b690bce73814000000'), u'date_creation': datetime.datetime(2010, 1, 26, 10, 32, 22, 497000)}
If no document is found, one()
returns None
find_random()
will return a random document from the database. This method doesn't take other arguments.
There are 4 methods to query your data which return Document
instances:
find()
, find_one()
, fetch()
, fetch_one
and find_random()
.
find()
and fetch()
return a cursor of collection. A cursor is a container
which lazy evaluate the results. A cursor is acting like an iterator.
find_one()
and fetch_one()
return the document itself.
All theses method can take a query as argument. A query is a simple dict. Please, see the mongodb and the pymongo documentation for further details.
find()
without argument will return a cursor of all documents of the collection.
If a query is passed, it will return a cursor all documents which match the query.
find()
takes the same arguments as the pymongo.collection.find
method.
>>> for post in tutorial.BlogPost.find():
... print post['title']
my first blog post
my second blog post
>>> for post in tutorial.BlogPost.find({'title':'my first blog post'}):
... print post['title']
my first blog post
find_one()
acts like find()
but will return only the first document found. This
method takes the same arguments as pymongo's find_one()
method. Please, check
the pymongo documentation.
one()
acts like find_one()
but will raise a mongokit.MultipleResultsFound
exception if
there is more than one result.
>>> tutorial.BlogPost.one()
Traceback (most recent call last):
...
MultipleResultsFound: 2 results found
>>> doc = tutorial.BlogPost.one({'title':'my first blog post'})
>>> doc
{u'body': None, u'title': u'my first blog post', u'author': u'myself', u'rank': 0, u'_id': ObjectId('4b5ec4b690bce73814000000'), u'date_creation': datetime.datetime(2010, 1, 26, 10, 32, 22, 497000)}
>>> isinstance(doc, BlogPost)
True
If no document is found, one()
returns None
Unlike find()
, fetch()
will return only documents which match the structure of the Document.
>>> all_blog_posts = tutorial.BlogPost.fetch()
This will return only all blog post (which have 'title', 'body', 'author', 'date_creation', 'rank' as fields). This is an helper for:
>>> all_blog_posts = tutorial.BlogPost.find({'body': {'$exists': True}, 'title': {'$exists': True}, 'date_creation': {'$exists': True}, 'rank': {'$exists': True}, 'author': {'$exists': True}})
Note that like find()
and one()
, you can pass advanced queries:
>>> my_blog_posts = tutorial.BlogPost.fetch({'author':'myself'})
which is equivalent to:
>>> all_blog_posts = tutorial.BlogPost.find({'body': {'$exists': True}, 'title': {'$exists': True}, 'date_creation': {'$exists': True}, 'rank': {'$exists': True}, 'author': 'myself'})
Just like fetch()
but raise a mongokit.MultipleResultsFound
exception if
there is more than one result.
find_random()
will return a random document from the database. This method doesn't take other arguments.