@@ -5,13 +5,12 @@ Aggregation Framework Examples
5
5
.. default-domain:: mongodb
6
6
7
7
MongoDB provides flexible data aggregation functionality with the
8
- :dbcommand:`aggregate` command.
8
+ :dbcommand:`aggregate` command. For additional information about
9
+ aggregation consider the following resources:
9
10
10
- See also
11
-
12
- * :doc:`/applications/aggregation` overview document
13
- * :doc:`/reference/aggregation`
14
- * http://www.mongodb.org/display/DOCS/SQL+to+Aggregation+Framework+Mapping+Chart
11
+ - :doc:`/applications/aggregation`
12
+ - :doc:`/reference/aggregation`
13
+ - :wiki:`SQL+to+Aggregation+Framework+Mapping+Chart`
15
14
16
15
This document provides a number of practical examples that display the
17
16
capabilities of the aggregation framework. All examples use a publicly
@@ -21,18 +20,18 @@ States.
21
20
Requirements
22
21
------------
23
22
24
- #. :program:`mongod` and :program:`mongo`, version 2.2 or later.
23
+ :program:`mongod` and :program:`mongo`, version 2.2 or later.
25
24
26
- Example Set One : Zip Codes
27
- ----------------------
25
+ Aggregations using the Zip Code Data Set
26
+ ----------------------------------------
28
27
29
28
To run you will need the zipcode data set. These data are available at:
30
- `media.mongodb.org/zips.json <http://media.mongodb.org/zips.json>`_.
31
- Use :program:`mongoimport` to load this data set into your
32
- :program:`mongod` instance.
29
+ `media.mongodb.org/zips.json <http://media.mongodb.org/zips.json>`_.
30
+ Use :program:`mongoimport` to load this data set into your
31
+ :program:`mongod` instance.
33
32
34
33
Data Model
35
- ~~~~~~~~~
34
+ ~~~~~~~~~~
36
35
37
36
Each document in this collection has the following form:
38
37
@@ -69,7 +68,7 @@ documentation for your :doc:`driver </applications/drivers>` for a
69
68
more idiomatic interface for data aggregation operations.
70
69
71
70
States with Populations Over 10 Million
72
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
72
74
73
To return all states with a population greater than 10 million, use
75
74
the following aggregation operation:
@@ -182,7 +181,6 @@ The final output of this aggregation operation is:
182
181
},
183
182
184
183
.. Will uncomment this section when we get the SQL
185
-
186
184
The equivalent :term:`SQL` for this operation is:
187
185
188
186
.. code-block:: sql
@@ -304,81 +302,111 @@ The final output of this aggregation operation is:
304
302
305
303
.. code-block:: sql
306
304
307
- Example Set Two : User Preferences
308
- ----------------------------
305
+ Aggregation with User Preference Data
306
+ -------------------------------------
307
+
308
+ Data Model
309
+ ~~~~~~~~~~
310
+
311
+ Consider a hypothetical data set of user preferences that that contains
312
+ sports information, with documents that resemble the following
313
+
314
+ .. code-block:: javascript
315
+
316
+ {
317
+ _id : "joe",
318
+ joined : ISODate("2012-07-02"),
319
+ likes : ["tennis", "golf", "fishing"]
320
+ }
321
+ {
322
+ _id : "jane",
323
+ joined : ISODate("2011-03-02"),
324
+ likes : ["golf"]
325
+ }
309
326
310
- Consider a hypothetical data set of user preferences for sports,
311
- similar to the following:
312
327
313
- { _id : "joe", joined : ISODate("2012-07-02"), likes : ["tennis",
314
- "golf", "fishing"] }
315
- { _id : "jane", joined : ISODate("2011-03-02", likes : ["golf"] }
316
- ...
328
+ Return a Single Field
329
+ ~~~~~~~~~~~~~~~~~~~~~
317
330
318
331
.. code-block:: javascript
319
332
320
- // fetch just the user names
321
- // this alone would be better done as a query with find(), but we will
322
- // build up from here.
323
- db.users.find.aggregate(
324
- [
325
- { $project : { _id:1 } }
326
- ]
327
- )
333
+ // fetch just the user names
334
+ // this alone would be better done as a query with find(), but we will
335
+ // build up from here.
336
+ db.users.find.aggregate(
337
+ [
338
+ { $project : { _id:1 } }
339
+ ]
340
+ )
341
+
342
+ Normalize and Sort Documents
343
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
328
344
329
345
.. code-block:: javascript
330
346
331
- // uppercase names with $toUpper operator to normalize their
332
- // case. Then show all names in sorted order.
333
- db.users.aggregate(
334
- [
335
- { $project : { name:{$toUpper:"$_id"} , _id:0 } },
336
- { $sort : { name : 1 } }
337
- ]
338
- )
347
+ // uppercase names with $toUpper operator to normalize their
348
+ // case. Then show all names in sorted order.
349
+ db.users.aggregate(
350
+ [
351
+ { $project : { name:{$toUpper:"$_id"} , _id:0 } },
352
+ { $sort : { name : 1 } }
353
+ ]
354
+ )
355
+
356
+ Determine Most Common Join Month in Collection
357
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
339
358
340
359
.. code-block:: javascript
341
360
342
- // show the top 4 months that people tend to join the club
343
- db.users.aggregate(
344
- [
345
- { $project : { month_joined : { $month : "$joined" } } },
346
- { $sort : { month_joined : 1 } },
347
- { $limit : 4 }
348
- ]
349
- )
361
+ // show the top 4 months that people tend to join the club
362
+ db.users.aggregate(
363
+ [
364
+ { $project : { month_joined : { $month : "$joined" } } },
365
+ { $sort : { month_joined : 1 } },
366
+ { $limit : 4 }
367
+ ]
368
+ )
369
+
370
+ Return Usernames Ordered by Join Month
371
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
350
372
351
373
.. code-block:: javascript
352
374
353
- // show user names ordered by the month they joined.
354
- // rename the "_id" field to the mnore descriptive fieldname "name"
355
- // while we are at it.
356
- db.users.aggregate(
357
- [
358
- { $project : { month_joined : { $month : "$joined" }, name : "$_id", _id : 0 } },
359
- { $sort : { month_joined : 1 } }
360
- ]
361
- )
375
+ // show user names ordered by the month they joined.
376
+ // rename the "_id" field to the mnore descriptive fieldname "name"
377
+ // while we are at it.
378
+ db.users.aggregate(
379
+ [
380
+ { $project : { month_joined : { $month : "$joined" }, name : "$_id", _id : 0 } },
381
+ { $sort : { month_joined : 1 } }
382
+ ]
383
+ )
384
+
385
+ Return Total Number of Joins per Month
386
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
362
387
363
388
.. code-block:: javascript
364
389
365
- // show for each month of the year, how many people joined in that month
366
- db.users.aggregate(
367
- [
368
- { $project : { month_joined : { $month : "$joined" } } } ,
369
- { $group : { _id : {month_joined:"$month_joined"} , n : { $sum : 1 } } },
370
- { $sort : { "_id.month_joined" : 1 } }
371
- ]
372
- )
390
+ // show for each month of the year, how many people joined in that month
391
+ db.users.aggregate(
392
+ [
393
+ { $project : { month_joined : { $month : "$joined" } } } ,
394
+ { $group : { _id : {month_joined:"$month_joined"} , n : { $sum : 1 } } },
395
+ { $sort : { "_id.month_joined" : 1 } }
396
+ ]
397
+ )
398
+
399
+ Return the Five Most Common "Likes"
400
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
373
401
374
402
.. code-block:: javascript
375
403
376
- // show the top five most liked activities, in ranked order
377
- db.users.aggregate(
378
- [
379
- { $unwind : "$likes" },
380
- { $group : { _id : "$likes" , n : { $sum : 1 } } },
381
- { $sort : { n : -1 } },
382
- { $limit : 5 }
383
- ]
384
- )
404
+ // show the top five most liked activities, in ranked order
405
+ db.users.aggregate(
406
+ [
407
+ { $unwind : "$likes" },
408
+ { $group : { _id : "$likes" , n : { $sum : 1 } } },
409
+ { $sort : { n : -1 } },
410
+ { $limit : 5 }
411
+ ]
412
+ )
0 commit comments