Skip to content

Commit fe183df

Browse files
committed
DOCSP-48165: Aggregation async examples
1 parent a235231 commit fe183df

13 files changed

+1078
-96
lines changed

source/aggregation.txt

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,48 @@ of New York. To do so, it uses an aggregation pipeline with the following stages
103103
documents by the ``borough`` field, accumulating a count of documents for each distinct
104104
value.
105105

106-
.. code-block:: python
107-
:copyable: true
106+
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
107+
corresponding code:
108108

109-
# Define an aggregation pipeline with a match stage and a group stage
110-
pipeline = [
111-
{ "$match": { "cuisine": "Bakery" } },
112-
{ "$group": { "_id": "$borough", "count": { "$sum": 1 } } }
113-
]
109+
.. tabs::
110+
111+
.. tab:: Synchronous
112+
:tabid: sync
113+
114+
.. code-block:: python
115+
:copyable: true
116+
117+
# Define an aggregation pipeline with a match stage and a group stage
118+
pipeline = [
119+
{ "$match": { "cuisine": "Bakery" } },
120+
{ "$group": { "_id": "$borough", "count": { "$sum": 1 } } }
121+
]
122+
123+
# Execute the aggregation
124+
aggCursor = collection.aggregate(pipeline)
125+
126+
# Print the aggregated results
127+
for document in aggCursor:
128+
print(document)
129+
130+
.. tab:: Asynchronous
131+
:tabid: async
132+
133+
.. code-block:: python
134+
:copyable: true
135+
136+
# Define an aggregation pipeline with a match stage and a group stage
137+
pipeline = [
138+
{ "$match": { "cuisine": "Bakery" } },
139+
{ "$group": { "_id": "$borough", "count": { "$sum": 1 } } }
140+
]
114141

115-
# Execute the aggregation
116-
aggCursor = collection.aggregate(pipeline)
142+
# Execute the aggregation
143+
aggCursor = await collection.aggregate(pipeline)
117144

118-
# Print the aggregated results
119-
for document in aggCursor:
120-
print(document)
145+
# Print the aggregated results
146+
async for document in aggCursor:
147+
print(document)
121148

122149
The preceding code example produces output similar to the following:
123150

source/aggregation/aggregation-tutorials.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,29 @@ pipeline in each tutorial.
6868

6969
Once you install the driver, create a file called
7070
``agg_tutorial.py``. Paste the following code in this file to create an
71-
app template for the aggregation tutorials:
71+
app template for the aggregation tutorials. Select the :guilabel:`Synchronous` or
72+
:guilabel:`Asynchronous` tab to see the corresponding code:
7273

7374
.. literalinclude:: /includes/aggregation/template-app.py
7475
:language: python
7576
:copyable: true
7677

78+
.. tabs::
79+
80+
.. tab:: Synchronous
81+
:tabid: sync
82+
83+
.. literalinclude:: /includes/aggregation/template-app.py
84+
:language: python
85+
:copyable: true
86+
87+
.. tab:: Asynchronous
88+
:tabid: async
89+
90+
.. literalinclude:: /includes/aggregation/aggregation-template-async.py
91+
:language: python
92+
:copyable: true
93+
7794
.. important::
7895

7996
In the preceding code, read the code comments to find the sections of

source/aggregation/aggregation-tutorials/filtered-subset.txt

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,30 @@ following code to the application:
5757
:dedent:
5858

5959
Delete any existing data in the collections and insert sample data into
60-
the ``persons`` collection as shown in the following code:
60+
the ``persons`` collection as shown in the following code. Select the
61+
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:
6162

62-
.. literalinclude:: /includes/aggregation/filtered-subset.py
63-
:language: python
64-
:copyable: true
65-
:start-after: start-insert-persons
66-
:end-before: end-insert-persons
67-
:dedent:
63+
.. tabs::
64+
65+
.. tab:: Synchronous
66+
:tabid: sync
67+
68+
.. literalinclude:: /includes/aggregation/filtered-subset.py
69+
:language: python
70+
:copyable: true
71+
:start-after: start-insert-persons
72+
:end-before: end-insert-persons
73+
:dedent:
74+
75+
.. tab:: Asynchronous
76+
:tabid: async
77+
78+
.. literalinclude:: /includes/aggregation/filtered-subset-async.py
79+
:language: python
80+
:copyable: true
81+
:start-after: start-insert-persons
82+
:end-before: end-insert-persons
83+
:dedent:
6884

6985
Tutorial
7086
--------
@@ -134,14 +150,30 @@ Tutorial
134150
.. step:: Run the aggregation pipeline
135151

136152
Add the following code to the end of your application to perform
137-
the aggregation on the ``persons`` collection:
138-
139-
.. literalinclude:: /includes/aggregation/filtered-subset.py
140-
:language: python
141-
:copyable: true
142-
:start-after: start-run-agg
143-
:end-before: end-run-agg
144-
:dedent:
153+
the aggregation on the ``persons`` collection. Select the :guilabel:`Synchronous` or
154+
:guilabel:`Asynchronous` tab to see the corresponding code:
155+
156+
.. tabs::
157+
158+
.. tab:: Synchronous
159+
:tabid: sync
160+
161+
.. literalinclude:: /includes/aggregation/filtered-subset.py
162+
:language: python
163+
:copyable: true
164+
:start-after: start-run-agg
165+
:end-before: end-run-agg
166+
:dedent:
167+
168+
.. tab:: Asynchronous
169+
:tabid: async
170+
171+
.. literalinclude:: /includes/aggregation/filtered-subset-async.py
172+
:language: python
173+
:copyable: true
174+
:start-after: start-run-agg
175+
:end-before: end-run-agg
176+
:dedent:
145177

146178
Finally, run the following command in your shell to start your
147179
application:

source/aggregation/aggregation-tutorials/group-total.txt

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,30 @@ following code to the application:
5959
:dedent:
6060

6161
Delete any existing data and insert sample data into
62-
the ``orders`` collection as shown in the following code:
62+
the ``orders`` collection as shown in the following code. Select the :guilabel:`Synchronous`
63+
or :guilabel:`Asynchronous` tab to see the corresponding code:
6364

64-
.. literalinclude:: /includes/aggregation/group-total.py
65-
:language: python
66-
:copyable: true
67-
:start-after: start-insert-orders
68-
:end-before: end-insert-orders
69-
:dedent:
65+
.. tabs::
66+
67+
.. tab:: Synchronous
68+
:tabid: sync
69+
70+
.. literalinclude:: /includes/aggregation/group-total.py
71+
:language: python
72+
:copyable: true
73+
:start-after: start-insert-orders
74+
:end-before: end-insert-orders
75+
:dedent:
76+
77+
.. tab:: Asynchronous
78+
:tabid: async
79+
80+
.. literalinclude:: /includes/aggregation/group-total-async.py
81+
:language: python
82+
:copyable: true
83+
:start-after: start-insert-orders
84+
:end-before: end-insert-orders
85+
:dedent:
7086

7187
Tutorial
7288
--------
@@ -166,14 +182,30 @@ Tutorial
166182
.. step:: Run the aggregation pipeline
167183

168184
Add the following code to the end of your application to perform
169-
the aggregation on the ``orders`` collection:
185+
the aggregation on the ``orders`` collection. Select the :guilabel:`Synchronous`
186+
or :guilabel:`Asynchronous` tab to see the corresponding code:
187+
188+
.. tabs::
170189

171-
.. literalinclude:: /includes/aggregation/group-total.py
172-
:language: python
173-
:copyable: true
174-
:start-after: start-run-agg
175-
:end-before: end-run-agg
176-
:dedent:
190+
.. tab:: Synchronous
191+
:tabid: sync
192+
193+
.. literalinclude:: /includes/aggregation/group-total.py
194+
:language: python
195+
:copyable: true
196+
:start-after: start-run-agg
197+
:end-before: end-run-agg
198+
:dedent:
199+
200+
.. tab:: Asynchronous
201+
:tabid: async
202+
203+
.. literalinclude:: /includes/aggregation/group-total-async.py
204+
:language: python
205+
:copyable: true
206+
:start-after: start-run-agg
207+
:end-before: end-run-agg
208+
:dedent:
177209

178210
Finally, run the following command in your shell to start your
179211
application:

source/aggregation/aggregation-tutorials/multi-field-join.txt

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,55 @@ collections by adding the following code to the application:
8181
:dedent:
8282

8383
Delete any existing data and insert sample data into
84-
the ``products`` collection as shown in the following code:
84+
the ``products`` collection as shown in the following code. Select the :guilabel:`Synchronous`
85+
or :guilabel:`Asynchronous` tab to see the corresponding code:
8586

86-
.. literalinclude:: /includes/aggregation/multi-field-join.py
87-
:language: python
88-
:copyable: true
89-
:start-after: start-insert-products
90-
:end-before: end-insert-products
91-
:dedent:
87+
.. tabs::
88+
89+
.. tab:: Synchronous
90+
:tabid: sync
91+
92+
.. literalinclude:: /includes/aggregation/multi-field-join.py
93+
:language: python
94+
:copyable: true
95+
:start-after: start-insert-products
96+
:end-before: end-insert-products
97+
:dedent:
98+
99+
.. tab:: Asynchronous
100+
:tabid: async
101+
102+
.. literalinclude:: /includes/aggregation/multi-field-join.py
103+
:language: python
104+
:copyable: true
105+
:start-after: start-insert-products
106+
:end-before: end-insert-products
107+
:dedent:
92108

93109
Delete any existing data and insert sample data into
94110
the ``orders`` collection as shown in the following code:
95111

96-
.. literalinclude:: /includes/aggregation/multi-field-join.py
97-
:language: python
98-
:copyable: true
99-
:start-after: start-insert-orders
100-
:end-before: end-insert-orders
101-
:dedent:
112+
.. tabs::
113+
114+
.. tab:: Synchronous
115+
:tabid: sync
116+
117+
.. literalinclude:: /includes/aggregation/multi-field-join.py
118+
:language: python
119+
:copyable: true
120+
:start-after: start-insert-orders
121+
:end-before: end-insert-orders
122+
:dedent:
123+
124+
.. tab:: Asynchronous
125+
:tabid: async
126+
127+
.. literalinclude:: /includes/aggregation/multi-field-join.py
128+
:language: python
129+
:copyable: true
130+
:start-after: start-insert-orders
131+
:end-before: end-insert-orders
132+
:dedent:
102133

103134
Tutorial
104135
--------
@@ -192,14 +223,30 @@ Tutorial
192223
.. step:: Run the aggregation pipeline
193224

194225
Add the following code to the end of your application to perform
195-
the aggregation on the ``products`` collection:
226+
the aggregation on the ``products`` collection. Select the :guilabel:`Synchronous`
227+
or :guilabel:`Asynchronous` tab to see the corresponding code:
196228

197-
.. literalinclude:: /includes/aggregation/multi-field-join.py
198-
:language: python
199-
:copyable: true
200-
:start-after: start-run-agg
201-
:end-before: end-run-agg
202-
:dedent:
229+
.. tabs::
230+
231+
.. tab:: Synchronous
232+
:tabid: sync
233+
234+
.. literalinclude:: /includes/aggregation/multi-field-join.py
235+
:language: python
236+
:copyable: true
237+
:start-after: start-run-agg
238+
:end-before: end-run-agg
239+
:dedent:
240+
241+
.. tab:: Asynchronous
242+
:tabid: async
243+
244+
.. literalinclude:: /includes/aggregation/multi-field-join.py
245+
:language: python
246+
:copyable: true
247+
:start-after: start-run-agg
248+
:end-before: end-run-agg
249+
:dedent:
203250

204251
Finally, run the following command in your shell to start your
205252
application:

0 commit comments

Comments
 (0)