Skip to content

Commit f9fb449

Browse files
authored
DOCSP-48162: Async examples for Write pages (#239)
1 parent 07f3b50 commit f9fb449

15 files changed

+1406
-348
lines changed

source/crud.txt

Lines changed: 289 additions & 75 deletions
Large diffs are not rendered by default.

source/crud/bulk-write.txt

Lines changed: 146 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,46 @@ Collection Bulk Write Example
266266

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

271-
.. io-code-block::
272-
:copyable:
272+
.. tabs::
273273

274-
.. input:: /includes/write/bulk-write.py
275-
:start-after: start-bulk-write-mixed-collection
276-
:end-before: end-bulk-write-mixed-collection
277-
:language: python
274+
.. tab:: Synchronous
275+
:tabid: sync
278276

279-
.. output::
280-
:visible: false
281-
282-
BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2,
283-
'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []},
284-
acknowledged=True)
277+
.. io-code-block::
278+
:copyable:
279+
280+
.. input:: /includes/write/bulk-write.py
281+
:start-after: start-bulk-write-mixed-collection
282+
:end-before: end-bulk-write-mixed-collection
283+
:language: python
284+
285+
.. output::
286+
:visible: false
287+
288+
BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2,
289+
'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []},
290+
acknowledged=True)
291+
292+
.. tab:: Asynchronous
293+
:tabid: async
294+
295+
.. io-code-block::
296+
:copyable:
297+
298+
.. input:: /includes/write/bulk-write-async.py
299+
:start-after: start-bulk-write-mixed-collection
300+
:end-before: end-bulk-write-mixed-collection
301+
:language: python
302+
303+
.. output::
304+
:visible: false
305+
306+
BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2,
307+
'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []},
308+
acknowledged=True)
285309

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

@@ -291,23 +315,48 @@ Client Bulk Write Example
291315
The following example performs multiple write operations on the
292316
``sample_restaurants.restaurants`` and ``sample_mflix.movies``
293317
namespaces by using the ``bulk_write()`` method on a ``MongoClient``
294-
instance:
295-
296-
.. io-code-block::
297-
:copyable:
298-
299-
.. input:: /includes/write/bulk-write.py
300-
:start-after: start-bulk-write-mixed-client
301-
:end-before: end-bulk-write-mixed-client
302-
:language: python
303-
304-
.. output::
305-
:visible: false
306-
307-
ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
308-
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1,
309-
'nModified': 1, 'nDeleted': 344, 'insertResults': {}, 'updateResults': {},
310-
'deleteResults': {}}, acknowledged=True, verbose=False)
318+
instance. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
319+
corresponding code:
320+
321+
.. tabs::
322+
323+
.. tab:: Synchronous
324+
:tabid: sync
325+
326+
.. io-code-block::
327+
:copyable:
328+
329+
.. input:: /includes/write/bulk-write.py
330+
:start-after: start-bulk-write-mixed-client
331+
:end-before: end-bulk-write-mixed-client
332+
:language: python
333+
334+
.. output::
335+
:visible: false
336+
337+
ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
338+
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1,
339+
'nModified': 1, 'nDeleted': 344, 'insertResults': {}, 'updateResults': {},
340+
'deleteResults': {}}, acknowledged=True, verbose=False)
341+
342+
.. tab:: Asynchronous
343+
:tabid: async
344+
345+
.. io-code-block::
346+
:copyable:
347+
348+
.. input:: /includes/write/bulk-write-async.py
349+
:start-after: start-bulk-write-mixed-client
350+
:end-before: end-bulk-write-mixed-client
351+
:language: python
352+
353+
.. output::
354+
:visible: false
355+
356+
ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
357+
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1,
358+
'nModified': 1, 'nDeleted': 344, 'insertResults': {}, 'updateResults': {},
359+
'deleteResults': {}}, acknowledged=True, verbose=False)
311360

312361
Customize Bulk Write Operations
313362
-------------------------------
@@ -363,14 +412,29 @@ to the ``Collection.bulk_write()`` method:
363412

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

368-
.. literalinclude:: /includes/write/bulk-write.py
369-
:start-after: start-bulk-write-unordered
370-
:end-before: end-bulk-write-unordered
371-
:language: python
372-
:copyable:
418+
.. tabs::
419+
420+
.. tab:: Synchronous
421+
:tabid: sync
422+
423+
.. literalinclude:: /includes/write/bulk-write.py
424+
:start-after: start-bulk-write-unordered
425+
:end-before: end-bulk-write-unordered
426+
:language: python
427+
:copyable:
373428

429+
.. tab:: Asynchronous
430+
:tabid: async
431+
432+
.. literalinclude:: /includes/write/bulk-write-async.py
433+
:start-after: start-bulk-write-unordered
434+
:end-before: end-bulk-write-unordered
435+
:language: python
436+
:copyable:
437+
374438
If any of the write operations in an unordered bulk write fail, {+driver-short+}
375439
reports the errors only after attempting all operations.
376440

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

437501
The following example calls the ``bulk_write()`` method from the preceding
438502
:ref:`pymongo-bulk-write-client-ex` but sets the ``verbose_results`` option
439-
to ``True``:
440-
441-
.. io-code-block::
442-
:copyable:
503+
to ``True``. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see
504+
the corresponding code:
505+
506+
.. tabs::
507+
508+
.. tab:: Synchronous
509+
:tabid: sync
510+
511+
.. io-code-block::
512+
:copyable:
513+
514+
.. input:: /includes/write/bulk-write.py
515+
:start-after: start-bulk-write-verbose
516+
:end-before: end-bulk-write-verbose
517+
:language: python
518+
519+
.. output::
520+
:visible: false
521+
522+
ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
523+
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1, 'nModified': 1,
524+
'nDeleted': 344, 'insertResults': {0: InsertOneResult(ObjectId('...'),
525+
acknowledged=True)}, 'updateResults': {1: UpdateResult({'ok': 1.0, 'idx': 1, 'n': 1,
526+
'nModified': 1}, acknowledged=True)}, 'deleteResults': {2: DeleteResult({'ok': 1.0,
527+
'idx': 2, 'n': 344}, acknowledged=True)}}, acknowledged=True, verbose=True)
528+
529+
.. tab:: Asynchronous
530+
:tabid: async
531+
532+
.. io-code-block::
533+
:copyable:
534+
535+
.. input:: /includes/write/bulk-write-async.py
536+
:start-after: start-bulk-write-verbose
537+
:end-before: end-bulk-write-verbose
538+
:language: python
539+
540+
.. output::
541+
:visible: false
542+
543+
ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
544+
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1, 'nModified': 1,
545+
'nDeleted': 344, 'insertResults': {0: InsertOneResult(ObjectId('...'),
546+
acknowledged=True)}, 'updateResults': {1: UpdateResult({'ok': 1.0, 'idx': 1, 'n': 1,
547+
'nModified': 1}, acknowledged=True)}, 'deleteResults': {2: DeleteResult({'ok': 1.0,
548+
'idx': 2, 'n': 344}, acknowledged=True)}}, acknowledged=True, verbose=True)
443549

444-
.. input:: /includes/write/bulk-write.py
445-
:start-after: start-bulk-write-verbose
446-
:end-before: end-bulk-write-verbose
447-
:language: python
448-
449-
.. output::
450-
:visible: false
451-
452-
ClientBulkWriteResult({'anySuccessful': True, 'error': None, 'writeErrors': [],
453-
'writeConcernErrors': [], 'nInserted': 1, 'nUpserted': 0, 'nMatched': 1, 'nModified': 1,
454-
'nDeleted': 344, 'insertResults': {0: InsertOneResult(ObjectId('...'),
455-
acknowledged=True)}, 'updateResults': {1: UpdateResult({'ok': 1.0, 'idx': 1, 'n': 1,
456-
'nModified': 1}, acknowledged=True)}, 'deleteResults': {2: DeleteResult({'ok': 1.0,
457-
'idx': 2, 'n': 344}, acknowledged=True)}}, acknowledged=True, verbose=True)
458550

459551
Return Values
460552
-------------

source/crud/delete.txt

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,55 @@ Delete One Document
6464
~~~~~~~~~~~~~~~~~~~
6565

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

69-
.. literalinclude:: /includes/write/delete.py
70-
:start-after: start-delete-one
71-
:end-before: end-delete-one
72-
:language: python
73-
:copyable:
70+
.. tabs::
71+
72+
.. tab:: Synchronous
73+
:tabid: sync
74+
75+
.. literalinclude:: /includes/write/delete.py
76+
:start-after: start-delete-one
77+
:end-before: end-delete-one
78+
:language: python
79+
:copyable:
80+
81+
.. tab:: Asynchronous
82+
:tabid: async
83+
84+
.. literalinclude:: /includes/write/delete-async.py
85+
:start-after: start-delete-one
86+
:end-before: end-delete-one
87+
:language: python
88+
:copyable:
7489

7590
Delete Multiple Documents
7691
~~~~~~~~~~~~~~~~~~~~~~~~~
7792

7893
The following example uses the ``delete_many()`` method to remove all documents
79-
in the ``restaurants`` collection with a ``borough`` value of ``"Brooklyn"``:
94+
in the ``restaurants`` collection with a ``borough`` value of ``"Brooklyn"``. Select the
95+
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:
96+
97+
.. tabs::
98+
99+
.. tab:: Synchronous
100+
:tabid: sync
101+
102+
.. literalinclude:: /includes/write/delete.py
103+
:start-after: start-delete-many
104+
:end-before: end-delete-many
105+
:language: python
106+
:copyable:
107+
108+
.. tab:: Asynchronous
109+
:tabid: async
80110

81-
.. literalinclude:: /includes/write/delete.py
82-
:start-after: start-delete-many
83-
:end-before: end-delete-many
84-
:language: python
85-
:copyable:
111+
.. literalinclude:: /includes/write/delete-async.py
112+
:start-after: start-delete-many
113+
:end-before: end-delete-many
114+
:language: python
115+
:copyable:
86116

87117
Customize the Delete Operation
88118
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -125,13 +155,28 @@ the delete operation.
125155

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

130-
.. literalinclude:: /includes/write/delete.py
131-
:start-after: start-delete-options
132-
:end-before: end-delete-options
133-
:language: python
134-
:copyable:
161+
.. tabs::
162+
163+
.. tab:: Synchronous
164+
:tabid: sync
165+
166+
.. literalinclude:: /includes/write/delete.py
167+
:start-after: start-delete-options
168+
:end-before: end-delete-options
169+
:language: python
170+
:copyable:
171+
172+
.. tab:: Asynchronous
173+
:tabid: async
174+
175+
.. literalinclude:: /includes/write/delete-async.py
176+
:start-after: start-delete-options
177+
:end-before: end-delete-options
178+
:language: python
179+
:copyable:
135180

136181
.. tip::
137182

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

152197
The following example performs the same delete operation as the previous example,
153-
but with a default collation of ``fr_CA``:
198+
but with a default collation of ``fr_CA``. Select the :guilabel:`Synchronous` or
199+
:guilabel:`Asynchronous` tab to see the corresponding code:
200+
201+
.. tabs::
202+
203+
.. tab:: Synchronous
204+
:tabid: sync
205+
206+
.. literalinclude:: /includes/write/delete.py
207+
:start-after: start-delete-many-collation
208+
:end-before: end-delete-many-collation
209+
:language: python
210+
:copyable:
211+
212+
.. tab:: Asynchronous
213+
:tabid: async
154214

155-
.. literalinclude:: /includes/write/delete.py
156-
:start-after: start-delete-many-collation
157-
:end-before: end-delete-many-collation
158-
:language: python
159-
:copyable:
215+
.. literalinclude:: /includes/write/delete-async.py
216+
:start-after: start-delete-many-collation
217+
:end-before: end-delete-many-collation
218+
:language: python
219+
:copyable:
160220

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

0 commit comments

Comments
 (0)