@@ -56,8 +56,11 @@ eg "$sort":
56
56
... {" $group" : {" _id" : " $tags" , " count" : {" $sum" : 1 }}},
57
57
... {" $sort" : SON([(" count" , - 1 ), (" _id" , - 1 )])}
58
58
... ]
59
- >>> list (db.things.aggregate(pipeline))
60
- [{u'count': 3, u'_id': u'cat'}, {u'count': 2, u'_id': u'dog'}, {u'count': 1, u'_id': u'mouse'}]
59
+ >>> import pprint
60
+ >>> pprint.pprint(list (db.things.aggregate(pipeline)))
61
+ [{u'_id': u'cat', u'count': 3},
62
+ {u'_id': u'dog', u'count': 2},
63
+ {u'_id': u'mouse', u'count': 1}]
61
64
62
65
To run an explain plan for this aggregation use the
63
66
:meth: `~pymongo.database.Database.command ` method::
@@ -118,7 +121,7 @@ iterate over the result collection:
118
121
119
122
>>> result = db.things.map_reduce(mapper, reducer, " myresults" )
120
123
>>> for doc in result.find():
121
- ... print doc
124
+ ... pprint.pprint( doc)
122
125
...
123
126
{u'_id': u'cat', u'value': 3.0}
124
127
{u'_id': u'dog', u'value': 2.0}
@@ -135,18 +138,23 @@ response to the map/reduce command, rather than just the result collection:
135
138
136
139
.. doctest ::
137
140
138
- >>> db.things.map_reduce(mapper, reducer, " myresults" , full_response = True )
139
- {u'counts': {u'input': 4, u'reduce': 2, u'emit': 6, u'output': 3}, u'timeMillis': ..., u'ok': ..., u'result': u'...'}
141
+ >>> pprint.pprint(
142
+ ... db.things.map_reduce(mapper, reducer, " myresults" , full_response = True ))
143
+ {u'counts': {u'emit': 6, u'input': 4, u'output': 3, u'reduce': 2},
144
+ u'ok': ...,
145
+ u'result': u'...',
146
+ u'timeMillis': ...}
140
147
141
148
All of the optional map/reduce parameters are also supported, simply pass them
142
149
as keyword arguments. In this example we use the `query ` parameter to limit the
143
150
documents that will be mapped over:
144
151
145
152
.. doctest ::
146
153
147
- >>> result = db.things.map_reduce(mapper, reducer, " myresults" , query = {" x" : {" $lt" : 2 }})
148
- >>> for doc in result.find():
149
- ... print doc
154
+ >>> results = db.things.map_reduce(
155
+ ... mapper, reducer, " myresults" , query= {" x" : {" $lt" : 2 }})
156
+ >>> for doc in results.find():
157
+ ... pprint.pprint(doc)
150
158
...
151
159
{u'_id': u'cat', u'value': 1.0}
152
160
{u'_id': u'dog', u'value': 1.0}
@@ -157,8 +165,16 @@ specify a different database to store the result collection:
157
165
.. doctest ::
158
166
159
167
>>> from bson.son import SON
160
- >>> db.things.map_reduce(mapper, reducer, out = SON([(" replace" , " results" ), (" db" , " outdb" )]), full_response = True )
161
- {u'counts': {u'input': 4, u'reduce': 2, u'emit': 6, u'output': 3}, u'timeMillis': ..., u'ok': ..., u'result': {u'db': ..., u'collection': ...}}
168
+ >>> pprint.pprint(
169
+ ... db.things.map_reduce(
170
+ ... mapper,
171
+ ... reducer,
172
+ ... out= SON([(" replace" , " results" ), (" db" , " outdb" )]),
173
+ ... full_response= True ))
174
+ {u'counts': {u'emit': 6, u'input': 4, u'output': 3, u'reduce': 2},
175
+ u'ok': ...,
176
+ u'result': {u'collection': ..., u'db': ...},
177
+ u'timeMillis': ...}
162
178
163
179
.. seealso :: The full list of options for MongoDB's `map reduce engine <http://www.mongodb.org/display/DOCS/MapReduce>`_
164
180
@@ -186,7 +202,7 @@ Here we are doing a simple group and count of the occurrences of ``x`` values:
186
202
...
187
203
>>> results = db.things.group(key = {" x" :1 }, condition = {}, initial = {" count" : 0 }, reduce = reducer)
188
204
>>> for doc in results:
189
- ... print doc
205
+ ... pprint.pprint( doc)
190
206
{u'count': 1.0, u'x': 1.0}
191
207
{u'count': 2.0, u'x': 2.0}
192
208
{u'count': 1.0, u'x': 3.0}
0 commit comments