Skip to content

Commit 1e47976

Browse files
committed
DOCSP-48165: Aggregation async examples
1 parent fd3e35a commit 1e47976

12 files changed

+1060
-95
lines changed

source/aggregation.txt

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

110-
.. code-block:: python
111-
:copyable: true
110+
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
111+
corresponding code:
112112

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

119-
# Execute the aggregation
120-
aggCursor = collection.aggregate(pipeline)
146+
# Execute the aggregation
147+
aggCursor = await collection.aggregate(pipeline)
121148

122-
# Print the aggregated results
123-
for document in aggCursor:
124-
print(document)
149+
# Print the aggregated results
150+
async for document in aggCursor:
151+
print(document)
125152

126153
The preceding code example produces output similar to the following:
127154

source/aggregation/filtered-subset.txt

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

5757
Delete any existing data in the collections and insert sample data into
58-
the ``persons`` collection as shown in the following code:
58+
the ``persons`` collection as shown in the following code. Select the
59+
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:
5960

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

6783
Tutorial
6884
--------
@@ -132,14 +148,30 @@ Tutorial
132148
.. step:: Run the aggregation pipeline
133149

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

144176
Finally, run the following command in your shell to start your
145177
application:

source/aggregation/group-total.txt

Lines changed: 46 additions & 14 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 and insert sample data into
60-
the ``orders`` collection as shown in the following code:
60+
the ``orders`` collection as shown in the following code. Select the :guilabel:`Synchronous`
61+
or :guilabel:`Asynchronous` tab to see the corresponding code:
6162

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

6985
Tutorial
7086
--------
@@ -164,14 +180,30 @@ Tutorial
164180
.. step:: Run the aggregation pipeline
165181

166182
Add the following code to the end of your application to perform
167-
the aggregation on the ``orders`` collection:
183+
the aggregation on the ``orders`` collection. Select the :guilabel:`Synchronous`
184+
or :guilabel:`Asynchronous` tab to see the corresponding code:
185+
186+
.. tabs::
168187

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

176208
Finally, run the following command in your shell to start your
177209
application:

source/aggregation/multi-field-join.txt

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

8181
Delete any existing data and insert sample data into
82-
the ``products`` collection as shown in the following code:
82+
the ``products`` collection as shown in the following code. Select the :guilabel:`Synchronous`
83+
or :guilabel:`Asynchronous` tab to see the corresponding code:
8384

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

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

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

101132
Tutorial
102133
--------
@@ -190,14 +221,30 @@ Tutorial
190221
.. step:: Run the aggregation pipeline
191222

192223
Add the following code to the end of your application to perform
193-
the aggregation on the ``products`` collection:
224+
the aggregation on the ``products`` collection. Select the :guilabel:`Synchronous`
225+
or :guilabel:`Asynchronous` tab to see the corresponding code:
194226

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

202249
Finally, run the following command in your shell to start your
203250
application:

0 commit comments

Comments
 (0)