Skip to content

Commit fc0ed78

Browse files
authored
(DOCSP-29215): CRUD > Bulk Updates page (#34)
# Pull Request Info [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - https://jira.mongodb.org/browse/DOCSP-29215 Staging - https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/docsp-2915-bulk/fundamentals/crud/write-operations/bulk/ ## Page Updated CRUD Operations > Write Operations > [Bulk Updates page](https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/crud/write-operations/bulk/) ## Self-Review Checklist - [ ] Is this free of any warnings or errors in the RST? - [ ] Did you run a spell-check? - [ ] Did you run a grammar-check? - [ ] Are all the links working?
1 parent e00f3ee commit fc0ed78

File tree

10 files changed

+251
-47
lines changed

10 files changed

+251
-47
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
2+
import com.mongodb.MongoBulkWriteException
3+
import com.mongodb.client.model.*
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import io.github.cdimascio.dotenv.dotenv
6+
import kotlinx.coroutines.flow.toList
7+
import kotlinx.coroutines.runBlocking
8+
import org.bson.codecs.pojo.annotations.BsonId
9+
import org.junit.jupiter.api.AfterAll
10+
import org.junit.jupiter.api.Assertions.*
11+
import org.junit.jupiter.api.BeforeAll
12+
import org.junit.jupiter.api.TestInstance
13+
import java.util.*
14+
import kotlin.test.*
15+
16+
// :snippet-start: bulk-data-model
17+
data class SampleDoc(
18+
@BsonId val id: Int,
19+
val x: Int? = null
20+
)
21+
// :snippet-end:
22+
23+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
24+
internal class BulkTest {
25+
companion object {
26+
val dotenv = dotenv()
27+
val client = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"])
28+
val database = client.getDatabase("sample_db")
29+
val collection = database.getCollection<SampleDoc>("sample_docs")
30+
31+
@BeforeAll
32+
@JvmStatic
33+
private fun beforeAll() {
34+
runBlocking {
35+
val sampleDocuments = listOf(
36+
SampleDoc(1),
37+
SampleDoc(2)
38+
)
39+
collection.insertMany(sampleDocuments)
40+
}
41+
}
42+
43+
@AfterAll
44+
@JvmStatic
45+
private fun afterAll() {
46+
runBlocking {
47+
collection.drop()
48+
client.close()
49+
}
50+
}
51+
}
52+
53+
@Test
54+
fun insertOperationTest() = runBlocking {
55+
// :snippet-start: insert-one
56+
val doc3 = InsertOneModel(SampleDoc(3))
57+
val doc4 = InsertOneModel(SampleDoc(4))
58+
// :snippet-end:
59+
// :snippet-start: bulk-write-exception
60+
val doc5 = InsertOneModel(SampleDoc(1))
61+
val doc6 = InsertOneModel(SampleDoc(3))
62+
try {
63+
val bulkOperations = listOf(
64+
(doc5),
65+
(doc6)
66+
)
67+
val bulkWrite = collection.bulkWrite(bulkOperations)
68+
assertFalse(bulkWrite.wasAcknowledged()) // :remove:
69+
} catch (e: MongoBulkWriteException) {
70+
println("A MongoBulkWriteException occurred with the following message: " + e.message)
71+
}
72+
// :snippet-end:
73+
// Junit test for the above code
74+
val expected = listOf(
75+
SampleDoc(1),
76+
SampleDoc(2)
77+
)
78+
assertEquals(expected, collection.find().toList())
79+
}
80+
81+
@Test
82+
fun replaceOneTest() = runBlocking {
83+
// :snippet-start: replace-one
84+
val filter = Filters.eq("_id", 1)
85+
val insert = SampleDoc(1, 4)
86+
val doc = ReplaceOneModel(filter, insert)
87+
// :snippet-end:
88+
// Junit test for the above code
89+
val insertTest = collection.bulkWrite(listOf(doc))
90+
assertTrue(insertTest.wasAcknowledged())
91+
}
92+
93+
@Test
94+
fun updateOneTest() = runBlocking {
95+
// :snippet-start: update-one
96+
val filter = Filters.eq("_id", 2)
97+
val update = Updates.set(SampleDoc::x.name, 8)
98+
val doc = UpdateOneModel<SampleDoc>(filter, update)
99+
// :snippet-end:
100+
// Junit test for the above code
101+
val updateTest = collection.bulkWrite(listOf(doc))
102+
assertTrue(updateTest.wasAcknowledged())
103+
}
104+
105+
@Test
106+
fun deleteOneTest() = runBlocking {
107+
// :snippet-start: delete
108+
val filter = Filters.eq("_id", 1)
109+
val doc = DeleteOneModel<SampleDoc>(filter)
110+
// :snippet-end:
111+
// Junit test for the above code
112+
val deleteTest = collection.bulkWrite(listOf(doc))
113+
assertTrue(deleteTest.wasAcknowledged())
114+
assertTrue(collection.find(filter).toList().isEmpty())
115+
}
116+
117+
@Test
118+
fun orderOfOperationsTest() = runBlocking {
119+
// :snippet-start: ordered
120+
val doc1: InsertOneModel<SampleDoc> = InsertOneModel(SampleDoc(3))
121+
val doc2: ReplaceOneModel<SampleDoc> = ReplaceOneModel(
122+
Filters.eq("_id", 1),
123+
SampleDoc(1, 2)
124+
)
125+
val doc3: UpdateOneModel<SampleDoc> =
126+
UpdateOneModel(
127+
Filters.eq("_id", 3),
128+
Updates.set(SampleDoc::x.name, 2)
129+
)
130+
val doc4: DeleteManyModel<SampleDoc> =
131+
DeleteManyModel(Filters.eq(SampleDoc::x.name, 2))
132+
133+
val bulkOperations = listOf(
134+
doc1,
135+
doc2,
136+
doc3,
137+
doc4
138+
)
139+
140+
val update = collection.bulkWrite(bulkOperations)
141+
// :snippet-end:
142+
// Junit test for the above code
143+
assertTrue(update.wasAcknowledged())
144+
}
145+
146+
@Test
147+
fun unorderedExecutionTest() = runBlocking {
148+
val doc1: InsertOneModel<SampleDoc> = InsertOneModel(SampleDoc(3))
149+
val doc2: ReplaceOneModel<SampleDoc> = ReplaceOneModel(
150+
Filters.eq("_id", 1),
151+
SampleDoc(1, 2)
152+
)
153+
val doc3: UpdateOneModel<SampleDoc> =
154+
UpdateOneModel(
155+
Filters.eq("_id", 3),
156+
Updates.set(SampleDoc::x.name, 2)
157+
)
158+
val doc4: DeleteManyModel<SampleDoc> =
159+
DeleteManyModel(Filters.eq(SampleDoc::x.name, 2))
160+
161+
val bulkOperations = listOf(
162+
doc1,
163+
doc2,
164+
doc3,
165+
doc4
166+
)
167+
// :snippet-start: unordered
168+
val options = BulkWriteOptions().ordered(false)
169+
val unorderedUpdate = collection.bulkWrite(bulkOperations, options)
170+
// :snippet-end:
171+
// Junit test for the above code
172+
assertTrue(unorderedUpdate.wasAcknowledged())
173+
}
174+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data class SampleDoc(
2+
@BsonId val id: Int,
3+
val x: Int? = null
4+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
val doc5 = InsertOneModel(SampleDoc(1))
2+
val doc6 = InsertOneModel(SampleDoc(3))
3+
try {
4+
val bulkOperations = listOf(
5+
(doc5),
6+
(doc6)
7+
)
8+
val bulkWrite = collection.bulkWrite(bulkOperations)
9+
} catch (e: MongoBulkWriteException) {
10+
println("A MongoBulkWriteException occurred with the following message: " + e.message)
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val filter = Filters.eq("_id", 1)
2+
val doc = DeleteOneModel<SampleDoc>(filter)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val doc3 = InsertOneModel(SampleDoc(3))
2+
val doc4 = InsertOneModel(SampleDoc(4))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
val doc1: InsertOneModel<SampleDoc> = InsertOneModel(SampleDoc(3))
2+
val doc2: ReplaceOneModel<SampleDoc> = ReplaceOneModel(
3+
Filters.eq("_id", 1),
4+
SampleDoc(1, 2)
5+
)
6+
val doc3: UpdateOneModel<SampleDoc> =
7+
UpdateOneModel(
8+
Filters.eq("_id", 3),
9+
Updates.set(SampleDoc::x.name, 2)
10+
)
11+
val doc4: DeleteManyModel<SampleDoc> =
12+
DeleteManyModel(Filters.eq(SampleDoc::x.name, 2))
13+
14+
val bulkOperations = listOf(
15+
doc1,
16+
doc2,
17+
doc3,
18+
doc4
19+
)
20+
21+
val update = collection.bulkWrite(bulkOperations)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val filter = Filters.eq("_id", 1)
2+
val insert = SampleDoc(1, 4)
3+
val doc = ReplaceOneModel(filter, insert)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val options = BulkWriteOptions().ordered(false)
2+
val unorderedUpdate = collection.bulkWrite(bulkOperations, options)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val filter = Filters.eq("_id", 2)
2+
val update = Updates.set(SampleDoc::x.name, 8)
3+
val doc = UpdateOneModel<SampleDoc>(filter, update)

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

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Overview
1616
--------
1717

1818
In this guide, you can learn how to use bulk operations in the
19-
MongoDB Java Driver.
19+
MongoDB Kotlin Driver.
2020

2121
To perform a create, replace, update, or delete operation,
2222
use its corresponding method. For example, to insert one document,
@@ -44,10 +44,15 @@ in a collection:
4444
{ "_id": 1 }
4545
{ "_id": 2 }
4646

47+
This data is modeled with the following Kotlin data class:
48+
49+
.. literalinclude:: /examples/generated/BulkTest.snippet.bulk-data-model.kt
50+
:language: kotlin
51+
4752
For more information about the methods and classes mentioned in this section,
4853
see the following API Documentation:
4954

50-
- `bulkWrite() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#bulkWrite(java.util.List,com.mongodb.client.model.BulkWriteOptions)>`__
55+
- `bulkWrite() <{+api+}mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/bulk-write.html>`__
5156
- `WriteModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/WriteModel.html>`__
5257
- `BulkWriteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html>`__
5358

@@ -64,11 +69,8 @@ Example
6469
The following example creates an ``InsertOneModel`` for two documents
6570
where the ``_id`` values are "3" and "4":
6671

67-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
68-
:language: java
69-
:dedent:
70-
:start-after: begin insertDocumentsExample
71-
:end-before: end insertDocumentsExample
72+
.. literalinclude:: /examples/generated/BulkTest.snippet.insert-one.kt
73+
:language: kotlin
7274

7375
.. important::
7476

@@ -79,29 +81,24 @@ where the ``_id`` values are "3" and "4":
7981
The following example tries to insert two documents where the ``_id`` is
8082
"1" and "3":
8183

82-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
83-
:language: java
84-
:dedent:
85-
:start-after: begin insertExceptionExample
86-
:end-before: end insertExceptionExample
84+
.. io-code-block::
8785

88-
The following shows the output of the preceding code:
89-
90-
.. code-block:: shell
91-
:copyable: false
86+
.. input:: /examples/generated/BulkTest.snippet.bulk-write-exception.kt
87+
:language: kotlin
9288

93-
A MongoBulkWriteException occurred with the following message:
94-
Bulk write operation error on server sample-shard-00-02.pw0q4.mongodb.net:27017.
95-
Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key
96-
error collection: crudOps.bulkWrite index: _id_ dup key: { _id: 1 }', details={}}].
89+
.. output::
90+
:language: console
91+
92+
A MongoBulkWriteException occurred with the following message:
93+
Bulk write operation error on server sample-shard-00-02.pw0q4.mongodb.net:27017.
94+
Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key
95+
error collection: crudOps.bulkWrite index: _id_ dup key: { _id: 1 }', details={}}].
9796

9897
To see why the document with the ``_id`` of "3" didn't insert, see
9998
the :ref:`Order of Execution <orderOfExecution>` section.
10099

101100
For more information about the methods and classes mentioned in this section,
102-
see the `InsertOneModel
103-
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/InsertOneModel.html>`__
104-
API Documentation.
101+
see the `InsertOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/InsertOneModel.html>`__ API Documentation.
105102

106103
Replace Operation
107104
~~~~~~~~~~~~~~~~~
@@ -124,11 +121,8 @@ The following example creates a ``ReplaceOneModel`` to
124121
replace a document where the ``_id`` is "1" with a document that
125122
contains an additional field:
126123

127-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
128-
:language: java
129-
:dedent:
130-
:start-after: begin replaceDocumentsExample
131-
:end-before: end replaceDocumentsExample
124+
.. literalinclude:: /examples/generated/BulkTest.snippet.replace-one.kt
125+
:language: kotlin
132126

133127
For more information about the methods and classes mentioned in this section,
134128
see the following resources:
@@ -161,11 +155,8 @@ The following example creates an ``UpdateOneModel`` to update
161155
a document where the ``_id`` is "2" to a document that
162156
contains an additional field:
163157

164-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
158+
.. literalinclude:: /examples/generated/BulkTest.snippet.update-one.kt
165159
:language: java
166-
:dedent:
167-
:start-after: begin updateDocumentsExample
168-
:end-before: end updateDocumentsExample
169160

170161
For more information about the methods and classes mentioned in this section,
171162
see the following resources:
@@ -197,11 +188,8 @@ Example
197188
The following example creates a ``DeleteOneModel`` to delete
198189
a document where the ``_id`` is "1":
199190

200-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
201-
:language: java
202-
:dedent:
203-
:start-after: begin deleteDocumentsExample
204-
:end-before: end deleteDocumentsExample
191+
.. literalinclude:: /examples/generated/BulkTest.snippet.delete.kt
192+
:language: kotlin
205193

206194
For more information about the methods and classes mentioned in this section,
207195
see the following API Documentation:
@@ -235,11 +223,8 @@ The following example performs these bulk operations:
235223
- An update operation for a document where the ``_id`` is "3" to a document that contains an additional field
236224
- A delete operation for all documents that contain the field ``x`` with the value "2"
237225

238-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
239-
:language: java
240-
:dedent:
241-
:start-after: begin bulkWriteExample
242-
:end-before: end bulkWriteExample
226+
.. literalinclude:: /examples/generated/BulkTest.snippet.ordered.kt
227+
:language: kotlin
243228

244229
After running this example, your collection contains the following
245230
document:
@@ -260,11 +245,8 @@ the bulk operation reports them at the end.
260245
Adding to the preceding example, including the following specifies the bulk
261246
operations to execute in any order:
262247

263-
.. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
264-
:language: java
265-
:dedent:
266-
:start-after: begin bulkWriteNotOrderedExample
267-
:end-before: end bulkWriteNotOrderedExample
248+
.. literalinclude:: /examples/generated/BulkTest.snippet.unordered.kt
249+
:language: kotlin
268250

269251
.. note::
270252

0 commit comments

Comments
 (0)