Skip to content

Commit f402779

Browse files
(DOCSP-29220): CRUD > Upsert page (#38)
* (DOCSP-29220): CRUD > Upsert * Update examples/src/test/kotlin/UpsertTest.kt Co-authored-by: Ben Perlmutter <[email protected]> --------- Co-authored-by: Ben Perlmutter <[email protected]>
1 parent 39a6a8d commit f402779

File tree

5 files changed

+128
-20
lines changed

5 files changed

+128
-20
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
import com.mongodb.client.model.Filters
3+
import com.mongodb.client.model.UpdateOptions
4+
import com.mongodb.client.model.Updates
5+
import com.mongodb.kotlin.client.coroutine.MongoClient
6+
import io.github.cdimascio.dotenv.dotenv
7+
import kotlinx.coroutines.runBlocking
8+
import org.bson.codecs.pojo.annotations.BsonId
9+
import org.bson.types.ObjectId
10+
import org.junit.jupiter.api.AfterAll
11+
import org.junit.jupiter.api.Assertions.*
12+
import org.junit.jupiter.api.BeforeAll
13+
import org.junit.jupiter.api.TestInstance
14+
import java.util.*
15+
import kotlin.test.*
16+
17+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
18+
internal class UpsertTest {
19+
20+
// :snippet-start: upsert-data-model
21+
data class PaintOrder(
22+
@BsonId val id: ObjectId = ObjectId(),
23+
val qty: Int,
24+
val color: String
25+
)
26+
// :snippet-end:
27+
28+
companion object {
29+
val dotenv = dotenv()
30+
val client = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"])
31+
val database = client.getDatabase("paint_store")
32+
val collection = database.getCollection<PaintOrder>("paint_order")
33+
34+
@BeforeAll
35+
@JvmStatic
36+
private fun beforeAll() {
37+
runBlocking {
38+
val paintOrders = listOf(
39+
PaintOrder(ObjectId(), 5, "red"),
40+
PaintOrder(ObjectId(), 8, "purple"),
41+
PaintOrder(ObjectId(), 0, "blue"),
42+
PaintOrder(ObjectId(), 0, "white"),
43+
PaintOrder(ObjectId(), 6, "yellow"),
44+
PaintOrder(ObjectId(), 0, "pink"),
45+
PaintOrder(ObjectId(), 0, "green"),
46+
PaintOrder(ObjectId(), 8, "black")
47+
)
48+
collection.insertMany(paintOrders)
49+
}
50+
}
51+
52+
@AfterAll
53+
@JvmStatic
54+
private fun afterAll() {
55+
runBlocking {
56+
collection.drop()
57+
client.close()
58+
}
59+
}
60+
}
61+
62+
@Test
63+
fun insertUpdateTest() = runBlocking {
64+
// :snippet-start: upsert-update
65+
val filter = Filters.eq(PaintOrder::color.name, "orange")
66+
val update = Updates.inc(PaintOrder::qty.name, 10)
67+
val options = UpdateOptions().upsert(true)
68+
69+
val results = collection.updateOne(filter, update, options)
70+
71+
println(results)
72+
// :snippet-end:
73+
// Junit test for the above code
74+
assertTrue(results.wasAcknowledged())
75+
assertEquals(0, results.modifiedCount)
76+
}
77+
78+
@Test
79+
fun updateOptionsTest() = runBlocking {
80+
// :snippet-start: no-options
81+
val filter = Filters.eq(PaintOrder::color.name, "orange")
82+
val update = Updates.inc(PaintOrder::qty.name, 10)
83+
84+
val results = collection.updateOne(filter, update)
85+
86+
println(results)
87+
// :snippet-end:
88+
assertTrue(results.wasAcknowledged())
89+
assertEquals(0, results.modifiedCount)
90+
}
91+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val filter = Filters.eq(PaintOrder::color.name, "orange")
2+
val update = Updates.inc(PaintOrder::qty.name, 10)
3+
4+
val results = collection.updateOne(filter, update)
5+
6+
println(results)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data class PaintOrder(
2+
@BsonId val id: ObjectId = ObjectId(),
3+
val qty: Int,
4+
val color: String
5+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
val filter = Filters.eq(PaintOrder::color.name, "orange")
2+
val update = Updates.inc(PaintOrder::qty.name, 10)
3+
val options = UpdateOptions().upsert(true)
4+
5+
val results = collection.updateOne(filter, update, options)
6+
7+
println(results)

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Overview
1414
--------
1515

1616
In this guide, you can learn how to perform an **upsert** with the
17-
MongoDB Java driver.
17+
MongoDB Kotlin driver.
1818

1919
Applications use insert and update operations to store and modify data.
2020
Sometimes, you need to choose between an insert and update depending on
@@ -50,6 +50,11 @@ colors of paint. The store had their annual online sale. Their
5050
{ "_id": { "$oid": "606b4cfbcd83be7518b958e0" }, "color": "green", "qty": 0 }
5151
{ "_id": { "$oid": "606b4cfbcd83be7518b958e1" }, "color": "black", "qty": 8 }
5252

53+
This data is modeled with the following Kotlin data class:
54+
55+
.. literalinclude:: /examples/generated/UpsertTest.snippet.upsert-data-model.kt
56+
:language: kotlin
57+
5358
The store received a fresh shipment and needs to update their inventory.
5459
The first item in the shipment is ten cans of orange paint.
5560

@@ -58,18 +63,15 @@ where the ``color`` is ``"orange"``, specify an update to ``increment`` the
5863
``qty`` field by ``10``, and specify ``true`` to
5964
``UpdateOptions.upsert()``:
6065

61-
.. literalinclude:: /includes/fundamentals/code-snippets/InsertUpdate.java
62-
:language: java
63-
:dedent:
64-
:start-after: begin updateOneExample
65-
:end-before: end updateOneExample
66+
.. io-code-block::
6667

67-
The method returns:
68+
.. input:: /examples/generated/UpsertTest.snippet.upsert-update.kt
69+
:language: kotlin
6870

69-
.. code-block:: json
70-
:copyable: false
71+
.. output::
72+
:language: console
7173

72-
AcknowledgedUpdateResult{ matchedCount=0, modifiedCount=0, upsertedId=BsonObjectId{ value=606b4cfc1601f9443b5d6978 }}
74+
AcknowledgedUpdateResult{ matchedCount=0, modifiedCount=0, upsertedId=BsonObjectId{ value=606b4cfc1601f9443b5d6978 }}
7375

7476
This ``AcknowledgedUpdateResult`` tells us:
7577

@@ -96,18 +98,15 @@ The following shows the documents in the ``paint_inventory`` collection:
9698

9799
Not including ``UpdateOptions`` results in no change to the collection.
98100

99-
.. literalinclude:: /includes/fundamentals/code-snippets/InsertUpdate.java
100-
:language: java
101-
:dedent:
102-
:start-after: begin updateOneAttemptExample
103-
:end-before: end updateOneAttemptExample
101+
.. io-code-block::
104102

105-
The method returns:
103+
.. input:: /examples/generated/UpsertTest.snippet.no-options.kt
104+
:language: kotlin
106105

107-
.. code-block:: json
108-
:copyable: false
109-
110-
AcknowledgedUpdateResult{ matchedCount=0, modifiedCount=0, upsertedId=null }
106+
.. output::
107+
:language: console
108+
109+
AcknowledgedUpdateResult{ matchedCount=0, modifiedCount=0, upsertedId=null }
111110

112111
For more information about the methods and classes mentioned in this section,
113112
see the following API Documentation:

0 commit comments

Comments
 (0)