|
| 1 | +.. uses aggregation.rst |
| 2 | + |
| 3 | +Aggregation Examples |
| 4 | +==================== |
| 5 | + |
| 6 | +There are several methods of performing aggregations in MongoDB. These |
| 7 | +examples cover the new aggregation framework, using map reduce and using the |
| 8 | +group method. |
| 9 | + |
| 10 | +.. code-block:: python |
| 11 | + |
| 12 | + from pymongo import MongoClient |
| 13 | + |
| 14 | + client = MongoClient() |
| 15 | + client.drop_database("aggregation_example") |
| 16 | + |
| 17 | +Setup |
| 18 | +----- |
| 19 | +To start, we'll insert some example data which we can perform |
| 20 | +aggregations on: |
| 21 | + |
| 22 | +.. code-block:: python |
| 23 | + |
| 24 | + >>> from pymongo import MongoClient |
| 25 | + >>> db = MongoClient().aggregation_example |
| 26 | + >>> result = db.things.insert_many( |
| 27 | + ... [ |
| 28 | + ... {"x": 1, "tags": ["dog", "cat"]}, |
| 29 | + ... {"x": 2, "tags": ["cat"]}, |
| 30 | + ... {"x": 2, "tags": ["mouse", "cat", "dog"]}, |
| 31 | + ... {"x": 3, "tags": []}, |
| 32 | + ... ] |
| 33 | + ... ) |
| 34 | + >>> result.inserted_ids |
| 35 | + [ObjectId('...'), ObjectId('...'), ObjectId('...'), ObjectId('...')] |
| 36 | + |
| 37 | +.. _aggregate-examples: |
| 38 | + |
| 39 | +Aggregation Framework |
| 40 | +--------------------- |
| 41 | + |
| 42 | +This example shows how to use the |
| 43 | +the ``~pymongo.collection.Collection.aggregate`` method method to use the aggregation |
| 44 | +framework. We'll perform a simple aggregation to count the number of |
| 45 | +occurrences for each tag in the ``tags`` array, across the entire collection. |
| 46 | +To achieve this we need to pass in three operations to the pipeline. |
| 47 | +First, we need to unwind the ``tags`` array, then group by the tags and |
| 48 | +sum them up, finally we sort by count. |
| 49 | + |
| 50 | +As python dictionaries don't maintain order you should use ``~bson.son.SON`` |
| 51 | +or ``collections.OrderedDict`` where explicit ordering is required |
| 52 | +eg "$sort": |
| 53 | + |
| 54 | +.. note:: |
| 55 | + |
| 56 | + aggregate requires server version **>= 2.1.0**. |
| 57 | + |
| 58 | +.. code-block:: python |
| 59 | + |
| 60 | + >>> from bson.son import SON |
| 61 | + >>> pipeline = [ |
| 62 | + ... {"$unwind": "$tags"}, |
| 63 | + ... {"$group": {"_id": "$tags", "count": {"$sum": 1}}}, |
| 64 | + ... {"$sort": SON([("count", -1), ("_id", -1)])}, |
| 65 | + ... ] |
| 66 | + >>> import pprint |
| 67 | + >>> pprint.pprint(list(db.things.aggregate(pipeline))) |
| 68 | + [{'_id': 'cat', 'count': 3}, |
| 69 | + {'_id': 'dog', 'count': 2}, |
| 70 | + {'_id': 'mouse', 'count': 1}] |
| 71 | + |
| 72 | +To run an explain plan for this aggregation use |
| 73 | +`PyMongoExplain <https://pypi.org/project/pymongoexplain/>`_, |
| 74 | +a companion library for PyMongo. It allows you to explain any CRUD operation |
| 75 | +by providing a few convenience classes: |
| 76 | + |
| 77 | +.. code-block:: python |
| 78 | + |
| 79 | + >>> from pymongoexplain import ExplainableCollection |
| 80 | + >>> ExplainableCollection(collection).aggregate(pipeline) |
| 81 | + {'ok': 1.0, 'queryPlanner': [...]} |
| 82 | + |
| 83 | +Or, use the the ``~pymongo.database.Database.command`` method method: |
| 84 | + |
| 85 | +.. code-block:: python |
| 86 | + |
| 87 | + >>> db.command('aggregate', 'things', pipeline=pipeline, explain=True) |
| 88 | + {'ok': 1.0, 'stages': [...]} |
| 89 | + |
| 90 | +As well as simple aggregations the aggregation framework provides projection |
| 91 | +capabilities to reshape the returned data. Using projections and aggregation, |
| 92 | +you can add computed fields, create new virtual sub-objects, and extract |
| 93 | +sub-fields into the top-level of results. |
| 94 | + |
| 95 | +.. seealso:: The full documentation for MongoDB's `aggregation framework |
| 96 | + <http://mongodb.com/docs/manual/applications/aggregation>`_ |
0 commit comments