@@ -96,7 +96,7 @@ Example
96
96
The following example creates an ``InsertOneModel`` for two documents
97
97
describing people:
98
98
99
- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
99
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ BulkWrite.java
100
100
:language: java
101
101
:dedent:
102
102
:start-after: begin insertDocumentsExample
@@ -111,7 +111,7 @@ describing people:
111
111
The following example tries to insert two documents where the ``_id`` is
112
112
``1`` and ``3``:
113
113
114
- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
114
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ BulkWrite.java
115
115
:language: java
116
116
:dedent:
117
117
:start-after: begin insertExceptionExample
@@ -156,7 +156,7 @@ The following example creates a ``ReplaceOneModel`` to
156
156
replace a document where the ``_id`` is ``1`` with a document that
157
157
contains an added ``location`` field:
158
158
159
- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
159
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ BulkWrite.java
160
160
:language: java
161
161
:dedent:
162
162
:start-after: begin replaceDocumentsExample
@@ -165,7 +165,7 @@ contains an added ``location`` field:
165
165
If multiple documents match the query filter specified in
166
166
the ``ReplaceOneModel`` instance, the operation replaces the first
167
167
result. You can specify a sort in a ``ReplaceOptions`` instance to apply
168
- an order to matched documents before the driver performs the replace
168
+ an order to matched documents before the server performs the replace
169
169
operation, as shown in the following code:
170
170
171
171
.. code-block:: java
@@ -207,7 +207,7 @@ Example
207
207
The following example creates an ``UpdateOneModel`` to update
208
208
the ``age`` field in a document where the ``_id`` is ``2``:
209
209
210
- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
210
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ BulkWrite.java
211
211
:language: java
212
212
:dedent:
213
213
:start-after: begin updateDocumentsExample
@@ -216,7 +216,7 @@ the ``age`` field in a document where the ``_id`` is ``2``:
216
216
If multiple documents match the query filter specified in
217
217
the ``UpdateOneModel`` instance, the operation updates the first
218
218
result. You can specify a sort in an ``UpdateOptions`` instance to apply
219
- an order to matched documents before the driver performs the update
219
+ an order to matched documents before the server performs the update
220
220
operation, as shown in the following code:
221
221
222
222
.. code-block:: java
@@ -260,7 +260,7 @@ Example
260
260
The following example creates a ``DeleteOneModel`` to delete
261
261
a document where the ``_id`` is ``1``:
262
262
263
- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
263
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ BulkWrite.java
264
264
:language: java
265
265
:dedent:
266
266
:start-after: begin deleteDocumentsExample
@@ -301,7 +301,7 @@ The following example performs these bulk operations:
301
301
changes the ``name`` to ``"Zaynab Hassan"``
302
302
- An operation that deletes all documents where the ``age`` value is greater than ``50``
303
303
304
- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
304
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ BulkWrite.java
305
305
:language: java
306
306
:dedent:
307
307
:start-after: begin bulkWriteExample
@@ -328,7 +328,7 @@ the bulk operation reports them at the end.
328
328
Adding to the preceding example, including the following specifies the bulk
329
329
operations to execute in any order:
330
330
331
- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
331
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ BulkWrite.java
332
332
:language: java
333
333
:dedent:
334
334
:start-after: begin bulkWriteNotOrderedExample
@@ -479,6 +479,8 @@ see the following API documentation:
479
479
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/bulk/ClientNamespacedWriteModel.html>`__
480
480
- `MongoNamespace <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoNamespace.html>`__
481
481
482
+ .. _java-sync-client-bulk-write-insert:
483
+
482
484
Insert Example
483
485
~~~~~~~~~~~~~~
484
486
@@ -488,63 +490,77 @@ the other document is inserted into the ``db.things`` collection.
488
490
The ``MongoNamespace`` instance defines the databases and collections that
489
491
each write operation applies to.
490
492
491
- .. code-block:: java
493
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ClientBulkWrite.java
494
+ :language: java
495
+ :dedent:
496
+ :start-after: start-insert-models
497
+ :end-before: end-insert-models
492
498
493
- MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
494
- MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
499
+ .. _java-sync-client-bulk-write-update:
495
500
496
- List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
501
+ Update Example
502
+ ~~~~~~~~~~~~~~
497
503
498
- bulkOperations.add(ClientNamespacedWriteModel
499
- .insertOne(
500
- peopleNamespace,
501
- new Document("name", "Julia Smith")
502
- )
503
- );
504
+ The following example shows how to use the ``bulkWrite()`` method to update
505
+ existing documents in the ``db.people`` and ``db.things`` collections:
504
506
505
- bulkOperations.add(ClientNamespacedWriteModel
506
- .insertOne(
507
- thingsNamespace,
508
- new Document("object", "washing machine")
509
- )
510
- );
507
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ClientBulkWrite.java
508
+ :language: java
509
+ :dedent:
510
+ :start-after: start-update-models
511
+ :end-before: end-update-models
511
512
512
- ClientBulkWriteResult result = mongoClient.bulkWrite(bulkOperations);
513
+ This example increments the value of the ``age`` field by ``1`` in the
514
+ document that has a ``name`` value of ``"Freya Polk"`` in the ``people``
515
+ collection. It also sets the value of the ``manufacturer`` field to
516
+ ``"Premium Technologies"`` in all documents that have a ``category``
517
+ value of ``"electronic"`` in the ``things`` collection.
513
518
514
- Replace Example
515
- ~~~~~~~~~~~~~~~
516
-
517
- The following example shows how to use the ``bulkWrite()`` method to replace
518
- existing documents in the ``db.people`` and ``db.things`` collections.
519
+ If multiple documents match the query filter specified in
520
+ a ``ClientNamespacedUpdateOneModel`` instance, the operation updates the
521
+ first result. You can specify a sort order in a `ClientUpdateOneOptions
522
+ <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/bulk/ClientUpdateOneOptions.html>`__
523
+ instance to apply an order to matched documents before the driver
524
+ performs the update operation, as shown in the following code:
519
525
520
526
.. code-block:: java
521
527
522
- MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
523
- MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
528
+ ClientUpdateOneOptions options = ClientUpdateOneOptions
529
+ .clientUpdateOneOptions()
530
+ .sort(Sorts.ascending("_id"));
524
531
525
- List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
532
+ .. _java-sync-client-bulk-write-replace:
526
533
527
- bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
528
- peopleNamespace,
529
- Filters.eq("_id", 1),
530
- new Document("name", "Frederic Hilbert")
531
- )
532
- );
534
+ Replace Example
535
+ ~~~~~~~~~~~~~~~
533
536
534
- bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
535
- thingsNamespace,
536
- Filters.eq("_id", 1),
537
- new Document("object", "potato")
538
- )
539
- );
537
+ The following example shows how to use the ``bulkWrite()`` method to replace
538
+ existing documents in the ``db.people`` and ``db.things`` collections:
540
539
541
- ClientBulkWriteResult result = mongoClient.bulkWrite(bulkOperations);
540
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ClientBulkWrite.java
541
+ :language: java
542
+ :dedent:
543
+ :start-after: start-replace-models
544
+ :end-before: end-replace-models
542
545
543
546
After this example runs successfully, the document that has an ``_id`` value of ``1``
544
547
in the ``people`` collection is replaced with a new document. The document in
545
548
the ``things`` collection that has an ``_id`` value of ``1``
546
549
is replaced with a new document.
547
550
551
+ If multiple documents match the query filter specified in
552
+ a ``ClientNamespacedReplaceOneModel`` instance, the operation replaces the
553
+ first result. You can specify a sort order in a `ClientReplaceOneOptions
554
+ <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/bulk/ClientReplaceOneOptions.html>`__
555
+ instance to apply an order to matched documents before the driver
556
+ performs the replace operation, as shown in the following code:
557
+
558
+ .. code-block:: java
559
+
560
+ ClientReplaceOneOptions options = ClientReplaceOneOptions
561
+ .clientReplaceOneOptions()
562
+ .sort(Sorts.ascending("_id"));
563
+
548
564
.. _java-sync-client-bulk-write-options:
549
565
550
566
Bulk Write Options
@@ -568,39 +584,11 @@ The following code sets the ``ordered()`` method on an
568
584
instance of ``ClientBulkWriteOptions`` and performs a bulk write operation to
569
585
insert multiple documents.
570
586
571
- .. code-block:: java
572
-
573
- MongoNamespace namespace = new MongoNamespace("db", "people");
574
-
575
- ClientBulkWriteOptions options = ClientBulkWriteOptions
576
- .clientBulkWriteOptions()
577
- .ordered(false);
578
-
579
- List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
580
-
581
- bulkOperations.add(
582
- ClientNamespacedWriteModel.insertOne(
583
- namespace,
584
- new Document("_id", 1).append("name", "Rudra Suraj")
585
- )
586
- );
587
-
588
- // Causes a duplicate key error
589
- bulkOperations.add(
590
- ClientNamespacedWriteModel.insertOne(
591
- namespace,
592
- new Document("_id", 1).append("name", "Mario Bianchi")
593
- )
594
- );
595
-
596
- bulkOperations.add(
597
- ClientNamespacedWriteModel.insertOne(
598
- namespace,
599
- new Document("name", "Wendy Zhang")
600
- )
601
- );
602
-
603
- ClientBulkWriteResult result = mongoClient.bulkWrite(bulkOperations, options);
587
+ .. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/ClientBulkWrite.java
588
+ :language: java
589
+ :dedent:
590
+ :start-after: start-order-exec
591
+ :end-before: end-order-exec
604
592
605
593
Even though the write operation inserting a document with a duplicate key results
606
594
in an error, the other operations are executed because the write operation is
0 commit comments