Skip to content

Commit 886a04c

Browse files
authored
DOCSP-46772: sort option for client bw - updateone & replaceone (#621)
* DOCSP-46772: sort option for client bw - updateone & replaceone * MW PR fixes 1 * move into sharedinclude * small fix
1 parent aa7e5ee commit 886a04c

File tree

5 files changed

+267
-83
lines changed

5 files changed

+267
-83
lines changed

source/crud/write-operations/bulk.txt

Lines changed: 68 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Example
9696
The following example creates an ``InsertOneModel`` for two documents
9797
describing people:
9898

99-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
99+
.. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/BulkWrite.java
100100
:language: java
101101
:dedent:
102102
:start-after: begin insertDocumentsExample
@@ -111,7 +111,7 @@ describing people:
111111
The following example tries to insert two documents where the ``_id`` is
112112
``1`` and ``3``:
113113

114-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
114+
.. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/BulkWrite.java
115115
:language: java
116116
:dedent:
117117
:start-after: begin insertExceptionExample
@@ -156,7 +156,7 @@ The following example creates a ``ReplaceOneModel`` to
156156
replace a document where the ``_id`` is ``1`` with a document that
157157
contains an added ``location`` field:
158158

159-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
159+
.. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/BulkWrite.java
160160
:language: java
161161
:dedent:
162162
:start-after: begin replaceDocumentsExample
@@ -165,7 +165,7 @@ contains an added ``location`` field:
165165
If multiple documents match the query filter specified in
166166
the ``ReplaceOneModel`` instance, the operation replaces the first
167167
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
169169
operation, as shown in the following code:
170170

171171
.. code-block:: java
@@ -207,7 +207,7 @@ Example
207207
The following example creates an ``UpdateOneModel`` to update
208208
the ``age`` field in a document where the ``_id`` is ``2``:
209209

210-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
210+
.. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/BulkWrite.java
211211
:language: java
212212
:dedent:
213213
:start-after: begin updateDocumentsExample
@@ -216,7 +216,7 @@ the ``age`` field in a document where the ``_id`` is ``2``:
216216
If multiple documents match the query filter specified in
217217
the ``UpdateOneModel`` instance, the operation updates the first
218218
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
220220
operation, as shown in the following code:
221221

222222
.. code-block:: java
@@ -260,7 +260,7 @@ Example
260260
The following example creates a ``DeleteOneModel`` to delete
261261
a document where the ``_id`` is ``1``:
262262

263-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
263+
.. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/BulkWrite.java
264264
:language: java
265265
:dedent:
266266
:start-after: begin deleteDocumentsExample
@@ -301,7 +301,7 @@ The following example performs these bulk operations:
301301
changes the ``name`` to ``"Zaynab Hassan"``
302302
- An operation that deletes all documents where the ``age`` value is greater than ``50``
303303

304-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
304+
.. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/BulkWrite.java
305305
:language: java
306306
:dedent:
307307
:start-after: begin bulkWriteExample
@@ -328,7 +328,7 @@ the bulk operation reports them at the end.
328328
Adding to the preceding example, including the following specifies the bulk
329329
operations to execute in any order:
330330

331-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
331+
.. literalinclude:: /includes/fundamentals/code-snippets/bulk-write/BulkWrite.java
332332
:language: java
333333
:dedent:
334334
:start-after: begin bulkWriteNotOrderedExample
@@ -479,6 +479,8 @@ see the following API documentation:
479479
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/bulk/ClientNamespacedWriteModel.html>`__
480480
- `MongoNamespace <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoNamespace.html>`__
481481

482+
.. _java-sync-client-bulk-write-insert:
483+
482484
Insert Example
483485
~~~~~~~~~~~~~~
484486

@@ -488,63 +490,77 @@ the other document is inserted into the ``db.things`` collection.
488490
The ``MongoNamespace`` instance defines the databases and collections that
489491
each write operation applies to.
490492

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
492498

493-
MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
494-
MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
499+
.. _java-sync-client-bulk-write-update:
495500

496-
List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
501+
Update Example
502+
~~~~~~~~~~~~~~
497503

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:
504506

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
511512

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.
513518

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:
519525

520526
.. code-block:: java
521527

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"));
524531

525-
List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
532+
.. _java-sync-client-bulk-write-replace:
526533

527-
bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
528-
peopleNamespace,
529-
Filters.eq("_id", 1),
530-
new Document("name", "Frederic Hilbert")
531-
)
532-
);
534+
Replace Example
535+
~~~~~~~~~~~~~~~
533536

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:
540539

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
542545

543546
After this example runs successfully, the document that has an ``_id`` value of ``1``
544547
in the ``people`` collection is replaced with a new document. The document in
545548
the ``things`` collection that has an ``_id`` value of ``1``
546549
is replaced with a new document.
547550

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+
548564
.. _java-sync-client-bulk-write-options:
549565

550566
Bulk Write Options
@@ -568,39 +584,11 @@ The following code sets the ``ordered()`` method on an
568584
instance of ``ClientBulkWriteOptions`` and performs a bulk write operation to
569585
insert multiple documents.
570586

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
604592

605593
Even though the write operation inserting a document with a duplicate key results
606594
in an error, the other operations are executed because the write operation is

source/crud/write-operations/modify.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ The following example demonstrates how to change the value of the
102102
If multiple documents match the query filter specified in
103103
the ``updateOne()`` method, it updates the first result. You can
104104
specify a sort in an ``UpdateOptions`` instance to apply an order to
105-
matched documents before the driver performs the update operation, as
105+
matched documents before the server performs the update operation, as
106106
shown in the following code:
107107

108108
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
@@ -234,7 +234,7 @@ The following shows the updated document:
234234
If multiple documents match the query filter specified in
235235
the ``replaceOne()`` method, it replaces the first result. You can
236236
specify a sort in a ``ReplaceOptions`` instance to apply an order to
237-
matched documents before the driver performs the replace operation, as
237+
matched documents before the server performs the replace operation, as
238238
shown in the following code:
239239

240240
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java

0 commit comments

Comments
 (0)