Skip to content

Commit e8af2a9

Browse files
authored
(DOCSP-29216): CRUD > Change a Doc page (#26)
* (DOCSP-29216): CRUD > Change a Doc page * Delete unused snippets * Refactor examples to use data class * Fix snippet * Optimize imports * Add updateOne() example to page
1 parent 89c98a3 commit e8af2a9

File tree

6 files changed

+167
-34
lines changed

6 files changed

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

source/fundamentals/crud/write-operations/change-a-document.txt

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ current inventory:
3737
{ "_id": 4, "color": "green", "qty": 6 }
3838
{ "_id": 5, "color": "pink", "qty": 0 }
3939

40+
This data is modeled with the following Kotlin data class:
41+
42+
.. literalinclude:: /examples/generated/ChangeTest.snippet.data-model.kt
43+
:language: kotlin
44+
4045
.. _update-operation:
4146

4247
.. _kotlin-fundamentals-update:
@@ -48,20 +53,20 @@ Update operations can change fields and values. They apply changes
4853
specified in an update document to one or more documents that match your
4954
query filter.
5055

51-
The `updateOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#updateOne(org.bson.conversions.Bson,org.bson.conversions.Bson)>`__
56+
The `updateOne() <TODO:(DOCSP-29169)>`__
5257
method changes the first document your query filter matches and the
53-
`updateMany() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#updateMany(org.bson.conversions.Bson,org.bson.conversions.Bson)>`__
58+
`updateMany() <TODO:(DOCSP-29169)>`__
5459
method changes all the documents your query filter matches.
5560

5661
You can call the ``updateOne()`` and ``updateMany()`` methods on a
5762
``MongoCollection`` instance as follows:
5863

59-
.. code-block:: java
60-
61-
collection.updateOne(query, updateDocument);
64+
.. code-block:: kotlin
6265

63-
collection.updateMany(query, updateDocument);
66+
collection.updateOne(query, updateDocument)
6467

68+
collection.updateMany(query, updateDocument)
69+
6570
Update Operation Parameters
6671
~~~~~~~~~~~~~~~~~~~~~~~~~~~
6772

@@ -74,38 +79,55 @@ parameters:
7479
You can create the ``updateDocument`` using an ``Updates`` builder as
7580
follows:
7681

77-
.. code-block:: java
82+
.. code-block:: kotlin
7883

79-
Bson updateDocument = Updates.operator(field, value);
84+
val updateDocument = Updates.operator(field, value)
8085

8186
See the MongoDB API documentation for a `complete list of
82-
Updates builders and their usage <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Updates.html>`__.
87+
Updates builders and their usage <TODO:(DOCSP-29169)>`__.
8388

8489
Example
8590
```````
8691

87-
The paint store receives a fresh shipment and needs to update their inventory.
88-
The shipment contains 20 cans of each paint color.
92+
The paint store needs to update their inventory after a customer returns a
93+
can of yellow paint.
94+
95+
To update the single can of paint, call the ``updateOne()`` method specifying
96+
the following:
97+
98+
- A query filter that matches the yellow color
99+
- An update document that contains instructions to increment the ``qty`` field by "1"
100+
101+
.. io-code-block::
102+
103+
.. input:: /examples/generated/ChangeTest.snippet.update-one.kt
104+
:language: kotlin
105+
106+
.. output::
107+
:language: console
108+
109+
Matched document count: 1
110+
Modified document count: 1
111+
112+
The paint store then receives a fresh shipment and needs to update their
113+
inventory again. The shipment contains 20 cans of each paint color.
89114

90115
To update the inventory, call the ``updateMany()`` method specifying the
91116
following:
92117

93118
- A query filter that matches all the colors
94119
- An update document that contains instructions to increment the ``qty`` field by "20"
95120

96-
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
97-
:language: java
98-
:dedent:
99-
:start-after: begin updateManyExample
100-
:end-before: end updateManyExample
121+
.. io-code-block::
101122

102-
Your output should look something like this:
123+
.. input:: /examples/generated/ChangeTest.snippet.update-many.kt
124+
:language: kotlin
103125

104-
.. code-block:: none
105-
:copyable: false
126+
.. output::
127+
:language: console
106128

107-
Matched document count: 5
108-
Modified document count: 5
129+
Matched document count: 5
130+
Modified document count: 5
109131

110132
The following shows the updated documents in the ``paint_inventory`` collection:
111133

@@ -140,17 +162,17 @@ A replace operation substitutes one document from your collection. The
140162
substitution occurs between a document your query filter matches and a
141163
replacement document.
142164

143-
The `replaceOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#replaceOne(org.bson.conversions.Bson,TDocument)>`__
165+
The `replaceOne() <TODO:(DOCSP-29169)>`__
144166
method removes all the existing fields and values in the
145167
matching document (except the ``_id`` field) and substitutes it with the
146168
replacement document.
147169

148170
You can call the ``replaceOne()`` method on a ``MongoCollection``
149171
instance as follows:
150172

151-
.. code-block:: java
173+
.. code-block:: kotlin
152174

153-
collection.replaceOne(query, replacementDocument);
175+
collection.replaceOne(query, replacementDocument)
154176

155177
Replace Operation Parameters
156178
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -172,19 +194,16 @@ following:
172194
- A query filter that matches documents where the ``color`` is "pink"
173195
- A replacement document where the ``color`` is "orange" and the ``qty`` is "25"
174196

175-
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
176-
:language: java
177-
:dedent:
178-
:start-after: begin replaceOneExample
179-
:end-before: end replaceOneExample
197+
.. io-code-block::
180198

181-
Your output should look something like this:
199+
.. input:: /examples/generated/ChangeTest.snippet.replace-one.kt
200+
:language: kotlin
182201

183-
.. code-block:: none
184-
:copyable: false
202+
.. output::
203+
:language: console
185204

186-
Matched document count: 1
187-
Modified document count: 1
205+
Matched document count: 1
206+
Modified document count: 1
188207

189208
The following shows the updated document:
190209

0 commit comments

Comments
 (0)