Skip to content

Commit d115a7e

Browse files
committed
DOCSP-46772: sort option for client bw - updateone & replaceone
1 parent e10923b commit d115a7e

File tree

5 files changed

+132
-5
lines changed

5 files changed

+132
-5
lines changed

examples/src/test/kotlin/ClientBulkTest.kt

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import com.mongodb.MongoNamespace
22
import com.mongodb.client.model.Filters
3+
import com.mongodb.client.model.Sorts
4+
import com.mongodb.client.model.Updates
35
import com.mongodb.client.model.bulk.ClientBulkWriteOptions
46
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
7+
import com.mongodb.client.model.bulk.ClientReplaceOneOptions
8+
import com.mongodb.client.model.bulk.ClientUpdateOneOptions
59
import com.mongodb.kotlin.client.coroutine.MongoClient
610
import config.getConfig
711
import kotlinx.coroutines.runBlocking
@@ -19,11 +23,14 @@ internal class ClientBulkTest {
1923
data class Person(
2024
@BsonId val id: Int,
2125
val name: String,
26+
val age: Int? = null,
2227
)
2328

2429
data class Object(
2530
@BsonId val id: Int,
2631
val type: String,
32+
val category: String? = null,
33+
val manufacturer: String? = null,
2734
)
2835
// :snippet-end:
2936

@@ -40,7 +47,12 @@ internal class ClientBulkTest {
4047
fun beforeAll() {
4148
runBlocking {
4249
personCollection.insertOne(Person(1, "Sandy King"))
43-
objectCollection.insertOne(Object(1, "artist easel"))
50+
personCollection.insertOne(Person(1,"Freya Polk",34))
51+
objectCollection.insertMany(listOf(
52+
Object(1, "artist easel"),
53+
Object(2, "keyboard", "electronic"),
54+
Object(3, "blender","electronic"),
55+
) )
4456
}
4557
}
4658

@@ -84,6 +96,37 @@ internal class ClientBulkTest {
8496
assertEquals(2, personCollection.countDocuments())
8597
}
8698

99+
100+
// Ignoring tests because successful completion of
101+
// writes is blocked on https://jira.mongodb.org/browse/CLOUDP-288992
102+
@Ignore
103+
fun updateOperationTest() = runBlocking {
104+
// :snippet-start: update-models
105+
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
106+
107+
docsToInsert.add(ClientNamespacedWriteModel
108+
.updateOne(
109+
MongoNamespace("sample_db", "people"),
110+
Filters.eq(Person::name.name, "Freya Polk"),
111+
Updates.inc(Person::age.name, 1)
112+
)
113+
)
114+
115+
docsToInsert.add(ClientNamespacedWriteModel
116+
.updateMany(
117+
MongoNamespace("sample_db", "objects"),
118+
Filters.eq(Object::category.name, "electronic"),
119+
Updates.set(Object::manufacturer.name, "Premium Technologies")
120+
)
121+
)
122+
123+
val clientBulkResult = client.bulkWrite(docsToInsert)
124+
// :snippet-end:
125+
126+
// Junit test for the above code
127+
assertEquals(3, clientBulkResult.modifiedCount)
128+
}
129+
87130
@Ignore
88131
fun replaceOperationTest() = runBlocking {
89132
// :snippet-start: replace-models
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
data class Person(
22
@BsonId val id: Int,
33
val name: String,
4+
val age: Int? = null,
45
)
56

67
data class Object(
78
@BsonId val id: Int,
89
val type: String,
10+
val category: String? = null,
11+
val manufacturer: String? = null,
912
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
2+
3+
docsToInsert.add(ClientNamespacedWriteModel
4+
.updateOne(
5+
MongoNamespace("sample_db", "people"),
6+
Filters.eq(Person::name.name, "Freya Polk"),
7+
Updates.inc(Person::age.name, 1)
8+
)
9+
)
10+
11+
docsToInsert.add(ClientNamespacedWriteModel
12+
.updateMany(
13+
MongoNamespace("sample_db", "objects"),
14+
Filters.eq(Object::category.name, "electronic"),
15+
Updates.set(Object::manufacturer.name, "Premium Technologies")
16+
)
17+
)
18+
19+
val clientBulkResult = client.bulkWrite(docsToInsert)

source/fundamentals/crud/write-operations/bulk.txt

+49-4
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ contains the additional ``location`` field:
159159
If multiple documents match the query filter specified in
160160
the ``ReplaceOneModel`` instance, the operation replaces the first
161161
result. You can specify a sort in a ``ReplaceOptions`` instance to apply
162-
an order to matched documents before the driver performs the replace
162+
an order to matched documents before the server performs the replace
163163
operation, as shown in the following code:
164164

165165
.. literalinclude:: /examples/generated/BulkTest.snippet.replace-model-options.kt
@@ -202,7 +202,7 @@ field by ``1`` in a document where the ``_id`` is ``2``:
202202
If multiple documents match the query filter specified in
203203
the ``UpdateOneModel`` instance, the operation updates the first
204204
result. You can specify a sort in an ``UpdateOptions`` instance to apply
205-
an order to matched documents before the driver performs the update
205+
an order to matched documents before the server performs the update
206206
operation, as shown in the following code:
207207

208208
.. literalinclude:: /examples/generated/BulkTest.snippet.update-model-options.kt
@@ -466,20 +466,65 @@ each write operation applies to.
466466
.. literalinclude:: /examples/generated/ClientBulkTest.snippet.insert-models.kt
467467
:language: kotlin
468468

469+
.. _kotlin-client-bulk-write-update:
470+
471+
Update Operation
472+
~~~~~~~~~~~~~~~~
473+
474+
The following example shows how to use the ``bulkWrite()`` method to update
475+
existing documents in the ``db.people`` and ``db.objects`` collections:
476+
477+
.. literalinclude:: /examples/generated/ClientBulkTest.snippet.update-models.kt
478+
:language: kotlin
479+
480+
This example increments the value of the ``age`` field by ``1`` in the
481+
document that has a ``name`` value of ``"Freya Polk"`` in the ``people``
482+
collection. It also sets the value of the ``manufacturer`` field to
483+
``"Premium Technologies"`` in all documents that have a ``category``
484+
value of ``"electronic"`` in the ``objects`` collection.
485+
486+
If multiple documents match the query filter specified in
487+
a ``ClientNamespacedUpdateOneModel`` instance, the operation updates the
488+
first result. You can specify a sort order in a `ClientUpdateOneOptions
489+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/bulk/ClientUpdateOneOptions.html>`__
490+
instance to apply an order to matched documents before the server
491+
performs the update operation, as shown in the following code:
492+
493+
.. code-block:: kotlin
494+
495+
val options = ClientUpdateOneOptions
496+
.clientUpdateOneOptions()
497+
.sort(Sorts.ascending("_id"))
498+
499+
.. _kotlin-client-bulk-write-replace:
500+
469501
Replace Operation
470502
~~~~~~~~~~~~~~~~~
471503

472504
The following example shows how to use the ``bulkWrite()`` method to replace
473-
existing documents in the ``sample_db.people`` and ``sample_db.things`` collections.
505+
existing documents in the ``sample_db.people`` and ``sample_db.objects`` collections.
474506

475507
.. literalinclude:: /examples/generated/ClientBulkTest.snippet.replace-models.kt
476508
:language: kotlin
477509

478510
After this example runs successfully, the document that has an ``_id`` value of ``1``
479511
in the ``people`` collection is replaced with a new document. The document in
480-
the ``things`` collection that has an ``_id`` value of ``1``
512+
the ``objects`` collection that has an ``_id`` value of ``1``
481513
is replaced with a new document.
482514

515+
If multiple documents match the query filter specified in
516+
a ``ClientNamespacedReplaceOneModel`` instance, the operation replaces the
517+
first result. You can specify a sort order in a `ClientReplaceOneOptions
518+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/bulk/ClientReplaceOneOptions.html>`__
519+
instance to apply an order to matched documents before the driver
520+
performs the replace operation, as shown in the following code:
521+
522+
.. code-block:: kotlin
523+
524+
val options = ClientReplaceOneOptions
525+
.clientReplaceOneOptions()
526+
.sort(Sorts.ascending("_id"))
527+
483528
.. _kotlin-client-bulk-write-options:
484529

485530
Bulk Write Options

source/whats-new.txt

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ What's New
1212

1313
Learn what's new in:
1414

15+
* :ref:`Version 5.4 <kotlin-coroutine-version-5.4>`
1516
* :ref:`Version 5.3 <kotlin-coroutine-version-5.3>`
1617
* :ref:`Version 5.2 <kotlin-coroutine-version-5.2>`
1718
* :ref:`Version 5.1.3 <kotlin-coroutine-version-5.1.3>`
@@ -22,6 +23,22 @@ Learn what's new in:
2223
* :ref:`Version 4.11 <version-4.11>`
2324
* :ref:`Version 4.10 <version-4.10>`
2425

26+
.. _kotlin-coroutine-version-5.4:
27+
28+
What's New in 5.4
29+
-----------------
30+
31+
The 5.4 driver release includes the following changes, fixes,
32+
and features:
33+
34+
.. sharedinclude:: dbx/jvm/v5.4-wn-items.rst
35+
36+
.. replacement:: sort-option-link
37+
38+
the :ref:`kotlin-client-bulk-write-update` and
39+
:ref:`kotlin-client-bulk-write-replace` sections of the Bulk
40+
Operations guide
41+
2542
.. _kotlin-coroutine-version-5.3:
2643

2744
What's New in 5.3

0 commit comments

Comments
 (0)