This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
query.txt
132 lines (88 loc) · 4.19 KB
/
query.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
Query
=====
There are two ways to query a collection : getting raw data or ``Document`` instance.
Getting raw data
----------------
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 are four methods to query raw data : `find()` and
`find_one()`, `one()` and `find_random()`.
find() and find_one()
~~~~~~~~~~~~~~~~~~~~~
`find()`, and `find_one()` act like the similar pymongo's methods.
Please, see the pymongo documentation.
one()
~~~~~
`one()` acts 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()
~~~~~~~~~~~~~
`find_random()` will return a random document from the database. This method doesn't take any arguments.
Getting Document instance
-----------------------------
There are 5 methods to query your data which return ``Document`` instances:
`find()`, `find_one()`, `one()`, `fetch()`, `fetch_one()` and `find_random()`.
`find()` and `fetch()` return a cursor of collection. A cursor is a container
which lazily evaluates the results. A cursor is acting like an iterator.
`find_one()`,`one()` and `fetch_one()` return the document itself.
All these methods can take a query as an argument. A query is a simple dict. Check
the mongodb and the pymongo documentation for further details.
find()
------
`find()` without an argument will return a cursor of all documents from the collection.
If a query is passed, it will return a cursor of all documents matching 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()
----------
`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. Check
the pymongo documentation.
one()
-----
`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
fetch()
-------
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 with `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'})
fetch_one()
-----------
Just like `fetch()` but raise a `mongokit.MultipleResultsFound` exception if
there is more than one result.
find_random()
-------------
`find_random()` will return a random document from the database. This method doesn't take other arguments.