10000 DOCSP-46772: sort option for client bw - updateone & replaceone (#204) · mongodb/docs-kotlin@7f55b25 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f55b25

Browse files
authored
DOCSP-46772: sort option for client bw - updateone & replaceone (#204)
* DOCSP-46772: sort option for client bw - updateone & replaceone * formatting * wip
1 parent e10923b commit 7f55b25

File tree

8 files changed

+197
-49
lines changed

8 files changed

+197
-49
lines changed

examples/src/test/kotlin/ClientBulkTest.kt

Lines changed: 74 additions & 23 deletions
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,14 @@ 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(
52+
listOf(
53+
Object(1, "artist easel"),
54+
Object(2, "keyboard", "electronic"),
55+
Object(3, "blender", "electronic"),
56+
)
57+
)
4458
}
4559
}
4660

@@ -62,18 +76,20 @@ internal class ClientBulkTest {
6276
// :snippet-start: insert-models
6377
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
6478

65-
docsToInsert.add(ClientNamespacedWriteModel
66-
.insertOne(
67-
MongoNamespace("sample_db", "people"),
68-
Person(2, "Julia Smith")
69-
)
79+
docsToInsert.add(
80+
ClientNamespacedWriteModel
81+
.insertOne(
82+
MongoNamespace("sample_db", "people"),
83+
Person(2, "Julia Smith")
84+
)
7085
)
7186

72-
docsToInsert.add(ClientNamespacedWriteModel
73-
.insertOne(
74-
MongoNamespace("sample_db", "objects"),
75-
Object(2, "washing machine")
76-
)
87+
docsToInsert.add(
88+
ClientNamespacedWriteModel
89+
.insertOne(
90+
MongoNamespace("sample_db", "objects"),
91+
Object(2, "washing machine")
92+
)
7793
)
7894

7995
val clientBulkResult = client.bulkWrite(docsToInsert)
@@ -84,25 +100,60 @@ internal class ClientBulkTest {
84100
assertEquals(2, personCollection.countDocuments())
85101
}
86102

103+
104+
// Ignoring tests because successful completion of
105+
// writes is blocked on https://jira.mongodb.org/browse/CLOUDP-288992
106+
@Ignore
107+
fun updateOperationTest() = runBlocking {
108+
// :snippet-start: update-models
109+
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
110+
111+
docsToInsert.add(
112+
ClientNamespacedWriteModel
113+
.updateOne(
114+
MongoNamespace("sample_db", "people"),
115+
Filters.eq(Person::name.name, "Freya Polk"),
116+
Updates.inc(Person::age.name, 1)
117+
)
118+
)
119+
120+
docsToInsert.add(
121+
ClientNamespacedWriteModel
122+
.updateMany(
123+
MongoNamespace("sample_db", "objects"),
124+
Filters.eq(Object::category.name, "electronic"),
125+
Updates.set(Object::manufacturer.name, "Premium Technologies")
126+
)
127+
)
128+
129+
val clientBulkResult = client.bulkWrite(docsToInsert)
130+
// :snippet-end:
131+
132+
// Junit test for the above code
133+
assertEquals(3, clientBulkResult.modifiedCount)
134+
}
135+
87136
@Ignore
88137
fun replaceOperationTest() = runBlocking {
89138
// :snippet-start: replace-models
90139
val docsReplacements = mutableListOf<ClientNamespacedWriteModel>()
91140

92-
docsReplacements.add(ClientNamespacedWriteModel
93-
.replaceOne(
94-
MongoNamespace("sample_db", "people"),
95-
Filters.eq(Person::id.name, 1),
96-
Person(1, "Frederic Hilbert")
97-
)
141+
docsReplacements.add(
142+
ClientNamespacedWriteModel
143+
.replaceOne(
144+
MongoNamespace("sample_db", "people"),
145+
Filters.eq(Person::id.name, 1),
146+
Person(1, "Frederic Hilbert")
147+
)
98148
)
99149

100-
docsReplacements.add(ClientNamespacedWriteModel
101-
.replaceOne(
102-
MongoNamespace("sample_db", "objects"),
103-
Filters.eq(Object::id.name, 1),
104-
Object(1, "ironing board")
105-
)
150+
docsReplacements.add(
151+
ClientNamespacedWriteModel
152+
.replaceOne(
153+
MongoNamespace("sample_db", "objects"),
154+
Filters.eq(Object::id.name, 1),
155+
Object(1, "ironing board")
156+
)
106157
)
107158

108159
val clientBulkResult = client.bulkWrite(docsReplacements)
Lines changed: 3 additions & 0 deletions
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
)
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
22

3-
docsToInsert.add(ClientNamespacedWriteModel
4-
.insertOne(
5-
MongoNamespace("sample_db", "people"),
6-
Person(2, "Julia Smith")
7-
)
3+
docsToInsert.add(
4+
ClientNamespacedWriteModel
5+
.insertOne(
6+
MongoNamespace("sample_db", "people"),
7+
Person(2, "Julia Smith")
8+
)
89
)
910

10-
docsToInsert.add(ClientNamespacedWriteModel
11-
.insertOne(
12-
MongoNamespace("sample_db", "objects"),
13-
Object(2, "washing machine")
14-
)
11+
docsToInsert.add(
12+
ClientNamespacedWriteModel
13+
.insertOne(
14+
MongoNamespace("sample_db", "objects"),
15+
Object(2, "washing machine")
16+
)
1517
)
1618

1719
val clientBulkResult = client.bulkWrite(docsToInsert)
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
val docsReplacements = mutableListOf<ClientNamespacedWriteModel>()
22

3-
docsReplacements.add(ClientNamespacedWriteModel
4-
.replaceOne(
5-
MongoNamespace("sample_db", "people"),
6-
Filters.eq(Person::id.name, 1),
7-
Person(1, "Frederic Hilbert")
8-
)
3+
docsReplacements.add(
4+
ClientNamespacedWriteModel
5+
.replaceOne(
6+
MongoNamespace("sample_db", "people"),
7+
Filters.eq(Person::id.name, 1),
8+
Person(1, "Frederic Hilbert")
9+
)
910
)
1011

11-
docsReplacements.add(ClientNamespacedWriteModel
12-
.replaceOne(
13-
MongoNamespace("sample_db", "objects"),
14-
Filters.eq(Object::id.name, 1),
15-
Object(1, "ironing board")
16-
)
12+
docsReplacements.add(
13+
ClientNamespacedWriteModel
14+
.replaceOne(
15+
MongoNamespace("sample_db", "objects"),
16+
Filters.eq(Object::id.name, 1),
17+
Object(1, "ironing board")
18+
)
1719
)
1820

1921
val clientBulkResult = client.bulkWrite(docsReplacements)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
2+
3+
docsToInsert.add(
4+
ClientNamespacedWriteModel
5+
.updateOne(
6+
MongoNamespace("sample_db", "people"),
7+
Filters.eq(Person::name.name, "Freya Polk"),
8+
Updates.inc(Person::age.name, 1)
9+
)
10+
)
11+
12+
docsToInsert.add(
13+
ClientNamespacedWriteModel
14+
.updateMany(
15+
MongoNamespace("sample_db", "objects"),
16+
Filters.eq(Object::category.name, "electronic"),
17+
Updates.set(Object::manufacturer.name, "Premium Technologies")
18+
)
19+
)
20+
21+
val clientBulkResult = client.bulkWrite(docsToInsert)

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

Lines changed: 49 additions & 4 deletions
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 10000 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/fundamentals/transactions.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ and commit changes to existing documents:
106106

107107
.. sharedinclude:: dbx/transactions-parallelism.rst
108108

109+
.. replacement:: driver-specific-content
110+
111+
If you're using {+mdb-server+} v8.0 or later, you can perform
112+
write operations on multiple namespaces within a single
113+
transaction by using bulk write operations. To learn more, see the
114+
:ref:`kotlin-fundamentals-bulkwrite` guide.
115+
109116
Additional Information
110117
----------------------
111118

source/whats-new.txt

Lines changed: 17 additions & 0 deletions
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)
0