Skip to content

DOCSP-48162: Async examples for Write pages #239

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

Merged
merged 5 commits into from
Mar 28, 2025
Merged
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
364 changes: 289 additions & 75 deletions source/crud.txt

Large diffs are not rendered by default.

200 changes: 146 additions & 54 deletions source/crud/bulk-write.txt
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,46 @@ Collection Bulk Write Example

The following example performs multiple write operations on the
``restaurants`` collection by using the ``bulk_write()`` method
on a ``Collection`` instance:
on a ``Collection`` instance. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous`
tab to see the corresponding code:

.. io-code-block::
:copyable:
.. tabs::

.. input:: /includes/write/bulk-write.py
:start-after: start-bulk-write-mixed-collection
:end-before: end-bulk-write-mixed-collection
:language: python
.. tab:: Synchronous
:tabid: sync

.. output::
:visible: false

BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2,
'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []},
acknowledged=True)
.. io-code-block::
:copyable:

.. input:: /includes/write/bulk-write.py
:start-after: start-bulk-write-mixed-collection
:end-before: end-bulk-write-mixed-collection
:language: python

.. output::
:visible: false

BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2,
'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []},
acknowledged=True)

.. tab:: Asynchronous
:tabid: async

.. io-code-block::
:copyable:

.. input:: /includes/write/bulk-write-async.py
:start-after: start-bulk-write-mixed-collection
:end-before: end-bulk-write-mixed-collection
:language: python

.. output::
:visible: false

BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2,
'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []},
acknowledged=True)

.. _pymongo-bulk-write-client-ex:

Expand All @@ -291,23 +315,48 @@ Client Bulk Write Example
The following example performs multiple write operations on the
``sample_restaurants.restaurants`` and ``sample_mflix.movies``
namespaces by using the ``bulk_write()`` method on a ``MongoClient``
instance:

.. io-code-block::
:copyable:

.. input:: /includes/write/bulk-write.py
:start-after: start-bulk-write-mixed-client
:end-before: end-bulk-write-mixed-client
:language: python

.. output::
:visible: false

ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1,
'nModified': 1, 'nDeleted': 344, 'insertResults': {}, 'updateResults': {},
'deleteResults': {}}, acknowledged=True, verbose=False)
instance. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
corresponding code:

.. tabs::

.. tab:: Synchronous
:tabid: sync

.. io-code-block::
:copyable:

.. input:: /includes/write/bulk-write.py
:start-after: start-bulk-write-mixed-client
:end-before: end-bulk-write-mixed-client
:language: python

.. output::
:visible: false

ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1,
'nModified': 1, 'nDeleted': 344, 'insertResults': {}, 'updateResults': {},
'deleteResults': {}}, acknowledged=True, verbose=False)

.. tab:: Asynchronous
:tabid: async

.. io-code-block::
:copyable:

.. input:: /includes/write/bulk-write-async.py
:start-after: start-bulk-write-mixed-client
:end-before: end-bulk-write-mixed-client
:language: python

.. output::
:visible: false

ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1,
'nModified': 1, 'nDeleted': 344, 'insertResults': {}, 'updateResults': {},
'deleteResults': {}}, acknowledged=True, verbose=False)

Customize Bulk Write Operations
-------------------------------
Expand Down Expand Up @@ -363,14 +412,29 @@ to the ``Collection.bulk_write()`` method:

The following example calls the ``bulk_write()`` method from the preceding
:ref:`pymongo-bulk-write-collection-ex` but sets the ``ordered`` option
to ``False``:
to ``False``. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see
the corresponding code:

.. literalinclude:: /includes/write/bulk-write.py
:start-after: start-bulk-write-unordered
:end-before: end-bulk-write-unordered
:language: python
:copyable:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/write/bulk-write.py
:start-after: start-bulk-write-unordered
:end-before: end-bulk-write-unordered
:language: python
:copyable:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/write/bulk-write-async.py
:start-after: start-bulk-write-unordered
:end-before: end-bulk-write-unordered
:language: python
:copyable:

If any of the write operations in an unordered bulk write fail, {+driver-short+}
reports the errors only after attempting all operations.

Expand Down Expand Up @@ -436,25 +500,53 @@ to the ``MongoClient.bulk_write()`` method:

The following example calls the ``bulk_write()`` method from the preceding
:ref:`pymongo-bulk-write-client-ex` but sets the ``verbose_results`` option
to ``True``:

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

.. tabs::

.. tab:: Synchronous
:tabid: sync

.. io-code-block::
:copyable:

.. input:: /includes/write/bulk-write.py
:start-after: start-bulk-write-verbose
:end-before: end-bulk-write-verbose
:language: python

.. output::
:visible: false

ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1, 'nModified': 1,
'nDeleted': 344, 'insertResults': {0: InsertOneResult(ObjectId('...'),
acknowledged=True)}, 'updateResults': {1: UpdateResult({'ok': 1.0, 'idx': 1, 'n': 1,
'nModified': 1}, acknowledged=True)}, 'deleteResults': {2: DeleteResult({'ok': 1.0,
'idx': 2, 'n': 344}, acknowledged=True)}}, acknowledged=True, verbose=True)

.. tab:: Asynchronous
:tabid: async

.. io-code-block::
:copyable:

.. input:: /includes/write/bulk-write-async.py
:start-after: start-bulk-write-verbose
:end-before: end-bulk-write-verbose
:language: python

.. output::
:visible: false

ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1, 'nModified': 1,
'nDeleted': 344, 'insertResults': {0: InsertOneResult(ObjectId('...'),
acknowledged=True)}, 'updateResults': {1: UpdateResult({'ok': 1.0, 'idx': 1, 'n': 1,
'nModified': 1}, acknowledged=True)}, 'deleteResults': {2: DeleteResult({'ok': 1.0,
'idx': 2, 'n': 344}, acknowledged=True)}}, acknowledged=True, verbose=True)

.. input:: /includes/write/bulk-write.py
:start-after: start-bulk-write-verbose
:end-before: end-bulk-write-verbose
:language: python

.. output::
:visible: false

ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1, 'nModified': 1,
'nDeleted': 344, 'insertResults': {0: InsertOneResult(ObjectId('...'),
acknowledged=True)}, 'updateResults': {1: UpdateResult({'ok': 1.0, 'idx': 1, 'n': 1,
'nModified': 1}, acknowledged=True)}, 'deleteResults': {2: DeleteResult({'ok': 1.0,
'idx': 2, 'n': 344}, acknowledged=True)}}, acknowledged=True, verbose=True)

Return Values
-------------
Expand Down
108 changes: 84 additions & 24 deletions source/crud/delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,55 @@ Delete One Document
~~~~~~~~~~~~~~~~~~~

The following example uses the ``delete_one()`` method to remove a document in
the ``restaurants`` collection with a ``name`` value of ``"Ready Penny Inn"``:
the ``restaurants`` collection with a ``name`` value of ``"Ready Penny Inn"``. Select the
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:

.. literalinclude:: /includes/write/delete.py
:start-after: start-delete-one
:end-before: end-delete-one
:language: python
:copyable:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/write/delete.py
:start-after: start-delete-one
:end-before: end-delete-one
:language: python
:copyable:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/write/delete-async.py
:start-after: start-delete-one
:end-before: end-delete-one
:language: python
:copyable:

Delete Multiple Documents
~~~~~~~~~~~~~~~~~~~~~~~~~

The following example uses the ``delete_many()`` method to remove all documents
in the ``restaurants`` collection with a ``borough`` value of ``"Brooklyn"``:
in the ``restaurants`` collection with a ``borough`` value of ``"Brooklyn"``. Select the
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:

.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/write/delete.py
:start-after: start-delete-many
:end-before: end-delete-many
:language: python
:copyable:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/write/delete.py
:start-after: start-delete-many
:end-before: end-delete-many
:language: python
:copyable:
.. literalinclude:: /includes/write/delete-async.py
:start-after: start-delete-many
:end-before: end-delete-many
:language: python
:copyable:

Customize the Delete Operation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -125,13 +155,28 @@ the delete operation.

The following code uses the ``delete_many()`` method to delete all documents in
the ``restaurants`` collection with a ``name`` value that includes the string ``"Mongo"``.
It also uses the ``comment`` option to add a comment to the operation:
It also uses the ``comment`` option to add a comment to the operation. Select the
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:

.. literalinclude:: /includes/write/delete.py
:start-after: start-delete-options
:end-before: end-delete-options
:language: python
:copyable:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/write/delete.py
:start-after: start-delete-options
:end-before: end-delete-options
:language: python
:copyable:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/write/delete-async.py
:start-after: start-delete-options
:end-before: end-delete-options
:language: python
:copyable:

.. tip::

Expand All @@ -150,13 +195,28 @@ use.
.. include:: /includes/collation-description.rst

The following example performs the same delete operation as the previous example,
but with a default collation of ``fr_CA``:
but with a default collation of ``fr_CA``. Select the :guilabel:`Synchronous` or
:guilabel:`Asynchronous` tab to see the corresponding code:

.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/write/delete.py
:start-after: start-delete-many-collation
:end-before: end-delete-many-collation
:language: python
:copyable:

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/write/delete.py
:start-after: start-delete-many-collation
:end-before: end-delete-many-collation
:language: python
:copyable:
.. literalinclude:: /includes/write/delete-async.py
:start-after: start-delete-many-collation
:end-before: end-delete-many-collation
:language: python
:copyable:

.. include:: /includes/collation-override-note.rst

Expand Down
Loading
Loading