Skip to content

Commit 206b9a5

Browse files
committed
merge
2 parents cea4f67 + f0534e3 commit 206b9a5

35 files changed

+7321
-6
lines changed

source/faq.txt

Lines changed: 765 additions & 0 deletions
Large diffs are not rendered by default.

source/fundamentals.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. _pymongo-fundamentals:
2+
3+
============
4+
Fundamentals
5+
============
6+
7+
.. meta::
8+
:description: Learn how to use (+driver-short+} to execute operations on MongoDB.
9+
10+
.. toctree::
11+
:titlesonly:
12+
:maxdepth: 1
13+
14+
/fundamentals/authentication
15+
/fundamentals/csot
16+
/fundamentals/collations
17+
/fundamentals/databases-and-collections
18+
/fundamentals/dates-and-times
19+
/fundamentals/enterprise-authentication
20+
/fundamentals/gridfs
21+
/fundamentals/indexes
22+
/fundamentals/in-use-encryption
23+
/fundamentals/periodic-executors
24+
/fundamentals/type-hints
25+
26+
- :ref:`pymongo-auth`
27+
- :ref:`pymongo-csot`
28+
- :ref:`pymongo-collations`
29+
- :ref:`pymongo-databases-collections`
30+
- :ref:`pymongo-dates-times`
31+
- :ref:`pymongo-enterprise-auth`
32+
- :ref:`pymongo-gridfs`
33+
- :ref:`pymongo-indexes`
34+
- :ref:`pymongo-in-use-encryption`
35+
- :ref:`pymongo-periodic-executors`
36+
- :ref:`pymongo-type-hints`
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)