Skip to content

Commit 3c38f24

Browse files
committed
DOCSP-46772: sort option for client bw - updateone & replaceone
1 parent bd73524 commit 3c38f24

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

source/includes/write/client-bulk-write.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import com.mongodb.MongoNamespace
2+
import com.mongodb.client.model.Filters
23
import com.mongodb.client.model.Filters.eq
4+
import com.mongodb.client.model.Updates
35
import com.mongodb.client.model.bulk.ClientBulkWriteOptions
46
import com.mongodb.client.model.bulk.ClientBulkWriteResult
57
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
@@ -12,11 +14,13 @@ data class Restaurant(
1214
val name: String,
1315
val borough: String,
1416
val cuisine: String,
17+
val stars: Int? = null,
1518
)
1619

1720
data class Movie(
1821
val title: String,
19-
val year: Int
22+
val year: Int,
23+
val seen: Boolean? = null,
2024
)
2125
// end-data-classes
2226

@@ -39,6 +43,22 @@ fun main() {
3943
)
4044
// end-insert-models
4145

46+
// start-update-models
47+
val restaurantUpdate = ClientNamespacedWriteModel
48+
.updateOne(
49+
MongoNamespace("sample_restaurants", "restaurants"),
50+
Filters.eq(Restaurant::name.name, "Villa Berulia"),
51+
Updates.inc(Restaurant::stars.name, 1)
52+
)
53+
54+
val movieUpdate = ClientNamespacedWriteModel
55+
.updateMany(
56+
MongoNamespace("sample_mflix", "movies"),
57+
Filters.eq(Movie::title.name, "Carrie"),
58+
Updates.set(Movie::seen.name, true)
59+
)
60+
// end-update-models
61+
4262
// start-replace-models
4363
val restaurantReplacement = ClientNamespacedWriteModel
4464
.replaceOne(
@@ -102,7 +122,7 @@ fun main() {
102122
)
103123
)
104124

105-
val result= mongoClient
125+
val result = mongoClient
106126
.bulkWrite(bulkOps, options)
107127
// end-options
108128
}

source/write/bulk-write.txt

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Atlas </getting-started>` guide.
5555
The documents in these collections are modeled by the following
5656
{+language+} data classes:
5757

58-
.. literalinclude:: /includes/write/bulk.kt
58+
.. literalinclude:: /includes/write/client-bulk-write.kt
5959
:start-after: start-data-classes
6060
:end-before: end-data-classes
6161
:language: kotlin
@@ -138,7 +138,7 @@ The following example creates an instance of ``UpdateOneModel``:
138138
If multiple documents match the query filter specified in
139139
the ``UpdateOneModel`` instance, the operation updates the first
140140
result. You can specify a sort in an ``UpdateOptions`` instance to apply
141-
an order to matched documents before the driver performs the update
141+
an order to matched documents before the server performs the update
142142
operation, as shown in the following code:
143143

144144
.. code-block:: kotlin
@@ -181,7 +181,7 @@ The following example creates an instance of ``ReplaceOneModel``:
181181
If multiple documents match the query filter specified in
182182
the ``ReplaceOneModel`` instance, the operation replaces the first
183183
result. You can specify a sort in a ``ReplaceOptions`` instance to apply
184-
an order to matched documents before the driver performs the replace
184+
an order to matched documents before the server performs the replace
185185
operation, as shown in the following code:
186186

187187
.. code-block:: kotlin
@@ -467,8 +467,8 @@ Insert Operations
467467
~~~~~~~~~~~~~~~~~
468468

469469
This example shows how to create models that contain instructions to
470-
insert two documents. One document is inserted into the ``db.people``
471-
collection, and the other document is inserted into the ``db.things`` collection.
470+
insert two documents. One document is inserted into the ``sample_restaurants.restaurants``
471+
collection, and the other document is inserted into the ``sample_mflix.movies`` collection.
472472
The ``MongoNamespace`` instance defines the target database and collection that
473473
each write operation applies to.
474474

@@ -479,11 +479,49 @@ each write operation applies to.
479479
:copyable:
480480
:dedent:
481481

482+
.. _kotlin-sync-client-bulk-write-update:
483+
484+
Update Operation
485+
~~~~~~~~~~~~~~~~
486+
487+
The following example shows how to use the ``bulkWrite()`` method to update
488+
existing documents in the ``sample_restaurants.restaurants`` and
489+
``sample_mflix.movies`` collections:
490+
491+
.. literalinclude:: /includes/write/client-bulk-write.kt
492+
:start-after: start-update-models
493+
:end-before: end-update-models
494+
:language: kotlin
495+
:copyable:
496+
:dedent:
497+
498+
This example increments the value of the ``stars`` field by ``1`` in the
499+
document that has a ``name`` value of ``"Villa Berulia"`` in the
500+
``restaurants`` collection. It also sets the value of the ``seen`` field
501+
to ``true`` in all documents that have a ``title`` value of ``"Carrie"``
502+
in the ``movies`` collection.
503+
504+
If multiple documents match the query filter specified in
505+
a ``ClientNamespacedUpdateOneModel`` instance, the operation updates the
506+
first result. You can specify a sort order in a `ClientUpdateOneOptions
507+
<{+core-api+}/com/mongodb/client/model/bulk/ClientUpdateOneOptions.html>`__
508+
instance to apply an order to matched documents before the server
509+
performs the update operation, as shown in the following code:
510+
511+
.. code-block:: kotlin
512+
513+
val options = ClientUpdateOneOptions
514+
.clientUpdateOneOptions()
515+
.sort(Sorts.ascending("_id"))
516+
517+
.. _kotlin-sync-client-bulk-write-replace:
518+
482519
Replace Operations
483520
~~~~~~~~~~~~~~~~~~
484521

485522
The following example shows how to create models to replace
486-
existing documents in the ``db.people`` and ``db.things`` collections:
523+
existing documents in the ``sample_restaurants.restaurants`` and
524+
``sample_mflix.movies`` collections:
487525

488526
.. literalinclude:: /includes/write/client-bulk-write.kt
489527
:start-after: start-replace-models
@@ -493,9 +531,22 @@ existing documents in the ``db.people`` and ``db.things`` collections:
493531
:dedent:
494532

495533
After this example runs successfully, the document that has an ``_id`` value of ``1``
496-
in the ``people`` collection is replaced with a new document. The document in
497-
the ``things`` collection that has an ``_id`` value of ``1``
498-
is replaced with a new document.
534+
in the ``restaurants`` collection is replaced with a new document. The document in
535+
the ``movies`` collection that has an ``_id`` value of ``1``
536+
is also replaced with a new document.
537+
538+
If multiple documents match the query filter specified in
539+
a ``ClientNamespacedReplaceOneModel`` instance, the operation replaces the
540+
first result. You can specify a sort order in a `ClientReplaceOneOptions
541+
<{+core-api+}/com/mongodb/client/model/bulk/ClientReplaceOneOptions.html>`__
542+
instance to apply an order to matched documents before the driver
543+
performs the replace operation, as shown in the following code:
544+
545+
.. code-block:: kotlin
546+
547+
val options = ClientReplaceOneOptions
548+
.clientReplaceOneOptions()
549+
.sort(Sorts.ascending("_id"))
499550

500551
Perform the Bulk Operation
501552
~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)