8000 (DOCSP-29218): CRUD > Embedded Arrays page (#35) · nickldp/docs-kotlin@e00f3ee · GitHub
[go: up one dir, main page]

Skip to content

Commit e00f3ee

Browse files
authored
(DOCSP-29218): CRUD > Embedded Arrays page (mongodb#35)
# Pull Request Info [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - https://jira.mongodb.org/browse/DOCSP-29218 Staging - https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/docsp-29218-emb-array/fundamentals/crud/write-operations/embedded-arrays/ ## Update Page CRUD Operations > Write Operations > [Update Arrays in a Document](https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/crud/write-operations/embedded-arrays/) ## 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 ffc9937 commit e00f3ee

8 files changed

+193
-37
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
2+
import com.mongodb.client.model.*
3+
import com.mongodb.kotlin.client.coroutine.MongoClient
4+
import io.github.cdimascio.dotenv.dotenv
5+
import kotlinx.coroutines.flow.toList
6+
import kotlinx.coroutines.runBlocking
7+
import org.bson.codecs.pojo.annotations.BsonId
8+
import org.junit.jupiter.api.AfterAll
9+
import org.junit.jupiter.api.AfterEach
10+
import org.junit.jupiter.api.Assertions.*
11+
import org.junit.jupiter.api.BeforeEach
12+
import org.junit.jupiter.api.TestInstance
13+
import java.util.*
14+
import kotlin.test.*
15+
16+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
17+
internal class UpdateArraysTest {
18+
// :snippet-start: array-data-model
19+
data class PaintOrder(
20+
@BsonId val id: Int,
21+
val qty: List<Int>,
22+
val color: String
23+
)
24+
// :snippet-end:
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+
@AfterAll
33+
@JvmStatic
34+
fun afterAll() {
35+
runBlocking {
36+
client.close()
37+
}
38+
}
39+
}
40+
41+
@BeforeEach
42+
fun beforeEach() {
43+
runBlocking {
44+
val paintOrders =
45+
PaintOrder(1, listOf(8, 12, 18), "green")
46+
collection.insertOne(paintOrders)
47+
}
48+
}
49+
50+
@AfterEach
51+
fun afterEach() {
52+
runBlocking {
53+
collection.drop()
54+
}
55+
}
56+
57+
@Test
58+
fun updateTest() = runBlocking {
59+
// :snippet-start: specify-update
60+
val filter = Filters.eq("_id", 1)
61+
val update = Updates.push(PaintOrder::qty.name, 17)
62+
val options = FindOneAndUpdateOptions()
63+
.returnDocument(ReturnDocument.AFTER)
64+
val result = collection.findOneAndUpdate(filter, update, options)
65+
print(result)
66+
// :snippet-end:
67+
// Junit test for the above code
68+
val actual = collection.find(filter)
69+
val expected = listOf(
70+
PaintOrder(1, listOf(8, 12, 18, 17), "green"))
71+
assertEquals(expected, actual.toList())
72+
}
73+
74+
@Test
75+
fun updateManyTest() = runBlocking {
76+
// :snippet-start: update-many
77+
val filter = Filters.eq(PaintOrder::qty.name, 18)
78+
val update = Updates.inc("${PaintOrder::qty.name}.$", -3)
79+
val options = FindOneAndUpdateOptions()
80+
.returnDocument(ReturnDocument.AFTER)
81+
val result = collection.findOneAndUpdate(filter, update, options)
82+
print(result)
83+
// :snippet-end:
84+
// Junit test for the above code
85+
val actualFilter = Filters.eq("_id", 1)
86+
val actual = collection.find(actualFilter)
87+
val expected = listOf(
88+
PaintOrder(1, listOf(8, 12, 15), "green"))
89+
assertEquals(expected, actual.toList())
90+
}
91+
92+
@Test
93+
fun updateAllTest() = runBlocking {
94+
// :snippet-start: update-all
95+
val filter = Filters.eq("_id", 1)
96+
val update = Updates.mul("${PaintOrder::qty.name}.$[]", 2)
97+
val options = FindOneAndUpdateOptions()
98+
.returnDocument(ReturnDocument.AFTER)
99+
val result = collection.findOneAndUpdate(filter, update, options)
100+
println(result)
101+
// :snippet-end:
102+
// Junit test for the above code
103+
val actual = collection.find(filter)
104+
val expected = listOf(
105+
PaintOrder(1, listOf(16, 24, 36), "green"))
106+
assertEquals(expected, actual.toList())
107+
}
108+
109+
@Test
110+
fun matchMultipleTest() = runBlocking {
111+
// :snippet-start: match
112+
val filter = Filters.eq("_id", 1)
113+
val smallerFilter = Filters.lt("smaller", 15)
114+
val options = FindOneAndUpdateOptions()
115+
.returnDocument(ReturnDocument.AFTER)
116+
.arrayFilters(listOf(smallerFilter))
117+
val update = Updates.inc("${PaintOrder::qty.name}.$[smaller]", 5)
118+
val result = collection.findOneAndUpdate(filter, update, options)
119+
println(result)
120+
// :snippet-end:
121+
// Junit test for the above code
122+
val actual = collection.find(filter)
123+
val expected = listOf(
124+
PaintOrder(1, listOf(13, 17, 18), "green"))
125+
assertEquals(expected, actual.toList())
126+
}
127+
}
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 qty: List<Int>,
4+
val color: String
5+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
val filter = Filters.eq("_id", 1)
2+
val smallerFilter = Filters.lt("smaller", 15)
3+
val options = FindOneAndUpdateOptions()
4+
.returnDocument(ReturnDocument.AFTER)
5+
.arrayFilters(listOf(smallerFilter))
6+
val update = Updates.inc("${PaintOrder::qty.name}.$[smaller]", 5)
7+
val result = collection.findOneAndUpdate(filter, update, options)
8+
println(result)
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 qty: IntArray,
4+
val color: String
5+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val filter = Filters.eq("_id", 1)
2+
val update = Updates.push(PaintOrder::qty.name, 17)
3+
val options = FindOneAndUpdateOptions()
4+
.returnDocument(ReturnDocument.AFTER)
5+
val result = collection.findOneAndUpdate(filter, update, options)
6+
print(result)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val filter = Filters.eq("_id", 1)
2+
val update = Updates.mul("${PaintOrder::qty.name}.$[]", 2)
3+
val options = FindOneAndUpdateOptions()
4+
.returnDocument(ReturnDocument.AFTER)
5+
val result = collection.findOneAndUpdate(filter, update, options)
6+
println(result)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val filter = Filters.eq(PaintOrder::qty.name, 18)
2+
val update = Updates.inc("${PaintOrder::qty.name}.$", -3)
3+
val options = FindOneAndUpdateOptions()
4+
.returnDocument(ReturnDocument.AFTER)
5+
val result = collection.findOneAndUpdate(filter, update, options)
6+
print(result)

source/fundamentals/crud/write-operations/embedded-arrays.txt

Lines changed: 30 additions & 37 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 update arrays in a document with the
19-
MongoDB Java driver.
19+
MongoDB Kotlin driver.
2020

2121
To update an array, you must do the following:
2222

@@ -34,6 +34,11 @@ document:
3434

3535
{ "_id": 1, "color": "green", "qty": [8, 12, 18] }
3636

37+
This data is modeled with the following Kotlin data class:
38+
39+
.. literalinclude:: /examples/generated/EmbeddedArraysTest.snippet.array-data-model.kt
40+
:language: kotlin
41+
3742
The examples on this page use the ``findOneAndUpdate()`` method of the
3843
``MongoCollection`` class to retrieve and update the document. Each
3944
example uses an instance of the ``FindOneAndUpdateOptions`` class to
@@ -54,18 +59,15 @@ The following example performs these actions:
5459
- Query for the sample document
5560
- Append "17" to the ``qty`` array in the document that matches the query filter
5661

57-
.. literalinclude:: /includes/fundamentals/code-snippets/UpdateArray.java
58-
:language: java
59-
:dedent:
60-
:start-after: begin pushElementsExample
61-
:end-before: end pushElementsExample
62+
.. io-code-block::
6263

63-
The preceding example updates the original document to the following state:
64+
.. input:: /examples/generated/EmbeddedArraysTest.snippet.specify-update.kt
65+
:language: kotlin
6466

65-
.. code-block:: json
66-
:copyable: false
67+
.. output::
68+
:language: console
6769

68-
{ "_id": 1, "color": "green", "qty": [8, 12, 18, 17] }
70+
PaintOrder(id=1, qty=[8, 12, 18, 17], color=green)
6971

7072
Specifying Array Elements
7173
-------------------------
@@ -96,18 +98,15 @@ The following example performs these actions:
9698
- Query for a document with a ``qty`` field containing the value "18"
9799
- Decrement the first array value in the document that matches the query filter by "3"
98100

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

105-
The preceding example updates the original document to the following state:
103+
.. input:: /examples/generated/EmbeddedArraysTest.snippet.update-many.kt
104+
:language: kotlin
106105

107-
.. code-block:: json
108-
:copyable: false
106+
.. output::
107+
:language: console
109108

110-
{ "_id": 1, "color": "green", "qty": [8, 12, 15] }
109+
PaintOrder(id=1, qty=[8, 12, 15], color=green)
111110

112111
For more information about the methods and operators mentioned in this section,
113112
see the following resources:
@@ -128,18 +127,15 @@ The following example performs these actions:
128127
- Query for the sample document
129128
- Multiply array elements matching the query filter by "2"
130129

131-
.. literalinclude:: /includes/fundamentals/code-snippets/UpdateArray.java
132-
:language: java
133-
:dedent:
134-
:start-after: begin updateAllElementsExample
135-
:end-before: end updateAllElementsExample
130+
.. io-code-block::
136131

137-
The preceding example updates the original document to the following state:
132+
.. input:: /examples/generated/EmbeddedArraysTest.snippet.update-all.kt
133+
:language: kotlin
138134

139-
.. code-block:: json
140-
:copyable: false
135+
.. output::
136+
:language: console
141137

142-
{ "_id": 1, "color": "green", "qty": [16, 24, 36] }
138+
PaintOrder(id=1, qty=[16, 24, 36], color=green)
143139

144140
For more information about the methods and operators mentioned in this section,
145141
see the following resources:
@@ -168,18 +164,15 @@ The following example performs these actions:
168164
- Set an array filter to search for values less than "15"
169165
- Increment array elements matching the query filter by "5"
170166

171-
.. literalinclude:: /includes/fundamentals/code-snippets/UpdateArray.java
172-
:language: java
173-
:dedent:
174-
:start-after: begin updateValueOptionsExample
175-
:end-before: end updateValueOptionsExample
167+
.. io-code-block::
176168

177-
The preceding example updates the original document to the following state:
169+
.. input:: /examples/generated/EmbeddedArraysTest.snippet.match.kt
170+
:language: kotlin
178171

179-
.. code-block:: json
180-
:copyable: false
172+
.. output::
173+
:language: console
181174

182-
{ "_id": 1, "color": "green", "qty": [13, 17, 18] }
175+
PaintOrder(id=1, qty=[13, 17, 18], color=green)
183176

184177
For more information about the methods and operators mentioned in this section,
185178
see the following resources:

0 commit comments

Comments
 (0)
0