Skip to content

DOCSP-48165: Aggregation async examples #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions source/aggregation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,48 @@ of New York. To do so, it uses an aggregation pipeline with the following stages
documents by the ``borough`` field, accumulating a count of documents for each distinct
value.

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

# Define an aggregation pipeline with a match stage and a group stage
pipeline = [
{ "$match": { "cuisine": "Bakery" } },
{ "$group": { "_id": "$borough", "count": { "$sum": 1 } } }
]
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. code-block:: python
:copyable: true

# Define an aggregation pipeline with a match stage and a group stage
pipeline = [
{ "$match": { "cuisine": "Bakery" } },
{ "$group": { "_id": "$borough", "count": { "$sum": 1 } } }
]

# Execute the aggregation
aggCursor = collection.aggregate(pipeline)

# Print the aggregated results
for document in aggCursor:
print(document)

.. tab:: Asynchronous
:tabid: async

.. code-block:: python
:copyable: true

# Define an aggregation pipeline with a match stage and a group stage
pipeline = [
{ "$match": { "cuisine": "Bakery" } },
{ "$group": { "_id": "$borough", "count": { "$sum": 1 } } }
]

# Execute the aggregation
aggCursor = collection.aggregate(pipeline)
# Execute the aggregation
aggCursor = await collection.aggregate(pipeline)

# Print the aggregated results
for document in aggCursor:
print(document)
# Print the aggregated results
async for document in aggCursor:
print(document)

The preceding code example produces output similar to the following:

Expand Down
21 changes: 17 additions & 4 deletions source/aggregation/aggregation-tutorials.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,24 @@ pipeline in each tutorial.

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

.. literalinclude:: /includes/aggregation/template-app.py
:language: python
:copyable: true
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/aggregation/template-app.py
:language: python
:copyable: true

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/aggregation/aggregation-template-async.py
:language: python
:copyable: true

.. important::

Expand Down
62 changes: 47 additions & 15 deletions source/aggregation/aggregation-tutorials/filtered-subset.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,30 @@ following code to the application:
:dedent:

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

.. literalinclude:: /includes/aggregation/filtered-subset.py
:language: python
:copyable: true
:start-after: start-insert-persons
:end-before: end-insert-persons
:dedent:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/aggregation/filtered-subset.py
:language: python
:copyable: true
:start-after: start-insert-persons
:end-before: end-insert-persons
:dedent:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/aggregation/filtered-subset-async.py
:language: python
:copyable: true
:start-after: start-insert-persons
:end-before: end-insert-persons
:dedent:

Tutorial
--------
Expand Down Expand Up @@ -134,14 +150,30 @@ Tutorial
.. step:: Run the aggregation pipeline

Add the following code to the end of your application to perform
the aggregation on the ``persons`` collection:

.. literalinclude:: /includes/aggregation/filtered-subset.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:
the aggregation on the ``persons`` collection. Select the :guilabel:`Synchronous` or
:guilabel:`Asynchronous` tab to see the corresponding code:

.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/aggregation/filtered-subset.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/aggregation/filtered-subset-async.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:

Finally, run the following command in your shell to start your
application:
Expand Down
60 changes: 46 additions & 14 deletions source/aggregation/aggregation-tutorials/group-total.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,30 @@ following code to the application:
:dedent:

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

.. literalinclude:: /includes/aggregation/group-total.py
:language: python
:copyable: true
:start-after: start-insert-orders
:end-before: end-insert-orders
:dedent:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/aggregation/group-total.py
:language: python
:copyable: true
:start-after: start-insert-orders
:end-before: end-insert-orders
:dedent:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/aggregation/group-total-async.py
:language: python
:copyable: true
:start-after: start-insert-orders
:end-before: end-insert-orders
:dedent:

Tutorial
--------
Expand Down Expand Up @@ -166,14 +182,30 @@ Tutorial
.. step:: Run the aggregation pipeline

Add the following code to the end of your application to perform
the aggregation on the ``orders`` collection:
the aggregation on the ``orders`` collection. Select the :guilabel:`Synchronous`
or :guilabel:`Asynchronous` tab to see the corresponding code:

.. tabs::

.. literalinclude:: /includes/aggregation/group-total.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:
.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/aggregation/group-total.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/aggregation/group-total-async.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:

Finally, run the following command in your shell to start your
application:
Expand Down
87 changes: 67 additions & 20 deletions source/aggregation/aggregation-tutorials/multi-field-join.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,55 @@ collections by adding the following code to the application:
:dedent:

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

.. literalinclude:: /includes/aggregation/multi-field-join.py
:language: python
:copyable: true
:start-after: start-insert-products
:end-before: end-insert-products
:dedent:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/aggregation/multi-field-join.py
:language: python
:copyable: true
:start-after: start-insert-products
:end-before: end-insert-products
:dedent:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/aggregation/multi-field-join-async.py
:language: python
:copyable: true
:start-after: start-insert-products
:end-before: end-insert-products
:dedent:

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

.. literalinclude:: /includes/aggregation/multi-field-join.py
:language: python
:copyable: true
:start-after: start-insert-orders
:end-before: end-insert-orders
:dedent:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/aggregation/multi-field-join.py
:language: python
:copyable: true
:start-after: start-insert-orders
:end-before: end-insert-orders
:dedent:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/aggregation/multi-field-join-async.py
:language: python
:copyable: true
:start-after: start-insert-orders
:end-before: end-insert-orders
:dedent:

Tutorial
--------
Expand Down Expand Up @@ -192,14 +223,30 @@ Tutorial
.. step:: Run the aggregation pipeline

Add the following code to the end of your application to perform
the aggregation on the ``products`` collection:
the aggregation on the ``products`` collection. Select the :guilabel:`Synchronous`
or :guilabel:`Asynchronous` tab to see the corresponding code:

.. literalinclude:: /includes/aggregation/multi-field-join.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/aggregation/multi-field-join.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/aggregation/multi-field-join-async.py
:language: python
:copyable: true
:start-after: start-run-agg
:end-before: end-run-agg
:dedent:

Finally, run the following command in your shell to start your
application:
Expand Down
Loading
Loading