Skip to content

Commit 408c1f7

Browse files
author
Sam Kleinman
committed
DOCS-721: edits, largely structural
1 parent 018ead0 commit 408c1f7

File tree

1 file changed

+101
-73
lines changed

1 file changed

+101
-73
lines changed

source/tutorial/aggregation-examples.txt

Lines changed: 101 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ Aggregation Framework Examples
55
.. default-domain:: mongodb
66

77
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:
910

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`
1514

1615
This document provides a number of practical examples that display the
1716
capabilities of the aggregation framework. All examples use a publicly
@@ -21,18 +20,18 @@ States.
2120
Requirements
2221
------------
2322

24-
#. :program:`mongod` and :program:`mongo`, version 2.2 or later.
23+
:program:`mongod` and :program:`mongo`, version 2.2 or later.
2524

26-
Example Set One : Zip Codes
27-
----------------------
25+
Aggregations using the Zip Code Data Set
26+
----------------------------------------
2827

2928
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.
3332

3433
Data Model
35-
~~~~~~~~~
34+
~~~~~~~~~~
3635

3736
Each document in this collection has the following form:
3837

@@ -69,7 +68,7 @@ documentation for your :doc:`driver </applications/drivers>` for a
6968
more idiomatic interface for data aggregation operations.
7069

7170
States with Populations Over 10 Million
72-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7372

7473
To return all states with a population greater than 10 million, use
7574
the following aggregation operation:
@@ -182,7 +181,6 @@ The final output of this aggregation operation is:
182181
},
183182

184183
.. Will uncomment this section when we get the SQL
185-
186184
The equivalent :term:`SQL` for this operation is:
187185

188186
.. code-block:: sql
@@ -304,81 +302,111 @@ The final output of this aggregation operation is:
304302

305303
.. code-block:: sql
306304

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+
}
309326

310-
Consider a hypothetical data set of user preferences for sports,
311-
similar to the following:
312327

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+
~~~~~~~~~~~~~~~~~~~~~
317330

318331
.. code-block:: javascript
319332

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+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
328344

329345
.. code-block:: javascript
330346

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+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
339358

340359
.. code-block:: javascript
341360

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+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
350372

351373
.. code-block:: javascript
352374

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+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
362387

363388
.. code-block:: javascript
364389

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+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
373401

374402
.. code-block:: javascript
375403

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

Comments
 (0)