Skip to content

Commit 5484add

Browse files
authored
(DOCSP-29209): CRUD > Specify a Query page (#16)
* (DOCSP-29209): Specify a Query page * Optimize imports * Convert to Flow.collect { } and io-code blocks
1 parent 86063c0 commit 5484add

9 files changed

+203
-53
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import com.mongodb.client.model.Filters
2+
import com.mongodb.kotlin.client.coroutine.MongoClient
3+
import io.github.cdimascio.dotenv.dotenv
4+
import kotlinx.coroutines.flow.toList
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.collections.List
13+
import kotlin.test.*
14+
15+
// :snippet-start: query-data-model
16+
data class PaintOrder(
17+
@BsonId val id: Int,
18+
val qty: Int,
19+
val color: String,
20+
val vendor: List<String>,
21+
val rating: Int? = null
22+
)
23+
// :snippet-end:
24+
25+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
26+
internal class QueryDocumentTest {
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+
39+
val paintOrders = listOf(
40+
PaintOrder(1, 9, "red", listOf("A", "E")),
41+
PaintOrder(2, 8, "purple", listOf("B", "D", "F"), 5),
42+
PaintOrder(3, 5, "blue", listOf("A", "E")),
43+
PaintOrder(4, 6, "white", listOf("D"), 9),
44+
PaintOrder(5, 4, "yellow", listOf("A", "B")),
45+
PaintOrder(6, 3, "pink", listOf("C")),
46+
PaintOrder(7, 8, "green", listOf("C", "E"), 7),
47+
PaintOrder(8, 7, "black", listOf("A", "C", "D"))
48+
)
49+
collection.insertMany(paintOrders)
50+
}
51+
}
52+
53+
54+
@AfterAll
55+
@JvmStatic
56+
private fun afterAll() {
57+
runBlocking {
58+
collection.drop()
59+
client.close()
60+
}
61+
}
62+
}
63+
64+
65+
@Test
66+
fun comparisonTest() = runBlocking {
67+
// :snippet-start: comparison-filter
68+
val filter = Filters.gt("qty", 7)
69+
collection.find(filter).collect { println(it) }
70+
// :snippet-end:
71+
// Junit test for the above code
72+
val expected = listOf(
73+
PaintOrder(1, 9, "red", listOf("A", "E")),
74+
PaintOrder(2, 8, "purple", listOf("B", "D", "F"), 5),
75+
PaintOrder(7, 8, "green", listOf("C", "E"), 7)
76+
)
77+
assertEquals(expected, collection.find(filter).toList() )
78+
}
79+
80+
@Test
81+
fun logicalTest() = runBlocking {
82+
// :snippet-start: logical-filter
83+
val filter = Filters.and(Filters.lte("qty", 5), Filters.ne("color", "pink"))
84+
collection.find(filter).collect { println(it) }
85+
// :snippet-end:
86+
// Junit test for the above code
87+
val expected = listOf(
88+
PaintOrder(3, 5, "blue", listOf("A", "E")),
89+
PaintOrder(5, 4, "yellow", listOf("A", "B"))
90+
)
91+
assertEquals(expected, collection.find(filter).toList() )
92+
}
93+
94+
@Test
95+
fun arrayTest() = runBlocking {
96+
// :snippet-start: array-filter
97+
val filter = Filters.size("vendor", 3)
98+
collection.find(filter).collect { println(it) }
99+
// :snippet-end:
100+
// Junit test for the above code
101+
val expected = listOf(
102+
PaintOrder(2, 8, "purple", listOf("B", "D", "F"), 5),
103+
PaintOrder(8, 7, "black", listOf("A", "C", "D"))
104+
)
105+
assertEquals(expected, collection.find(filter).toList() )
106+
}
107+
108+
@Test
109+
fun elementTest() = runBlocking {
110+
// :snippet-start: element-filter
111+
val filter = Filters.exists("rating")
112+
collection.find(filter).collect { println(it) }
113+
// :snippet-end:
114+
// Junit test for the above code
115+
val expected = listOf(
116+
PaintOrder(2, 8, "purple", listOf("B", "D", "F"), 5),
117+
PaintOrder(4, 6, "white", listOf("D"), 9),
118+
PaintOrder(7, 8, "green", listOf("C", "E"), 7)
119+
)
120+
assertEquals(expected, collection.find(filter).toList() )
121+
}
122+
123+
@Test
124+
fun evaluationTest() = runBlocking {
125+
// :snippet-start: evaluation-filter
126+
val filter = Filters.regex("color", "k$")
127+
collection.find(filter).collect { println(it) }
128+
// :snippet-end:
129+
// Junit test for the above code
130+
val expected = listOf(
131+
PaintOrder(6, 3, "pink", listOf("C")),
132+
PaintOrder(8, 7, "black", listOf("A", "C", "D"))
133+
)
134+
assertEquals(expected, collection.find(filter).toList() )
135+
}
136+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// val filter = Filters.empty()
2+
// val pipeline = listOf(
3+
// Aggregates.match(filter),
4+
// Aggregates.group("\$color", Accumulators.sum("qty", "\$qty")),
5+
// Aggregates.sort(Sorts.descending("qty"))
6+
// )
7+
// collection.aggregate<Document>(pipeline).toList().forEach { println(it.toJson()) }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val filter = Filters.size("vendor", 3)
2+
collection.find(filter).collect { println(it) }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val filter = Filters.gt("qty", 7)
2+
collection.find(filter).collect { println(it) }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val filter = Filters.exists("rating")
2+
collection.find(filter).collect { println(it) }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val filter = Filters.regex("color", "k$")
2+
collection.find(filter).collect { println(it) }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val filter = Filters.and(Filters.lte("qty", 5), Filters.ne("color", "pink"))
2+
collection.find(filter).collect { println(it) }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
data class PaintOrder(
2+
@BsonId val id: Int,
3+
val qty: Int,
4+
val color: String,
5+
val vendor: List<String>,
6+
val rating: Int? = null
7+
)

source/fundamentals/crud/query-document.txt

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Specify a Query
1313
Overview
1414
--------
1515

16-
In this guide, you can learn how to specify a query in the MongoDB Java
16+
In this guide, you can learn how to specify a query in the MongoDB Kotlin
1717
driver.
1818

1919
Most CRUD operations allow you to narrow the set of matched documents by
@@ -44,6 +44,11 @@ The examples in this guide use the following documents in the
4444
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }
4545
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }
4646

47+
This data is modeled with the following Kotlin data class:
48+
49+
.. literalinclude:: /examples/generated/QueryDocumentTest.snippet.query-data-model.kt
50+
:language: kotlin
51+
4752
.. _query-comparison:
4853

4954
Comparison Operators
@@ -58,20 +63,17 @@ The following example uses the ``Filters.gt()`` method to match all
5863
documents where the value of ``qty`` is greater than ``7`` in the
5964
``paint_purchases`` collection:
6065

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

67-
The following shows the output of the preceding query:
68+
.. input:: /examples/generated/QueryDocumentTest.snippet.comparison-filter.kt
69+
:language: kotlin
6870

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

72-
{ "_id": 1, "color": "red", "qty": 9, "vendor": ["A", "E"] }
73-
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
74-
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }
74+
PaintOrder(id=1, qty=9, color=red, vendor=[A, E], rating=null)
75+
PaintOrder(id=2, qty=8, color=purple, vendor=[B, D, F], rating=5)
76+
PaintOrder(id=7, qty=8, color=green, vendor=[C, E], rating=7)
7577

7678
.. _query-logical:
7779

@@ -88,19 +90,16 @@ documents where the value of ``qty`` is less than or equal to ``5`` and
8890
the value of ``color`` is not ``"pink"`` in the ``paint_purchases``
8991
collection:
9092

91-
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
92-
:language: java
93-
:dedent:
94-
:start-after: begin logicalFilter
95-
:end-before: end logicalFilter
93+
.. io-code-block::
9694

97-
The following shows the output of the preceding query:
95+
.. input:: /examples/generated/QueryDocumentTest.snippet.logical-filter.kt
96+
:language: kotlin
9897

99-
.. code-block:: json
100-
:copyable: false
98+
.. output::
99+
:language: console
101100

102-
{ "_id": 3, "color": "blue", "qty": 5, "vendor": ["A", "E"] }
103-
{ "_id": 5, "color": "yellow", "qty": 4, "vendor": ["A", "B"] }
101+
PaintOrder(id=3, qty=5, color=blue, vendor=[A, E], rating=null)
102+
PaintOrder(id=5, qty=4, color=yellow, vendor=[A, B], rating=null)
104103

105104
.. _query-arrays:
106105

@@ -114,19 +113,16 @@ The following example uses the ``Filters.size()`` method to match
114113
documents where the size of the ``vendor`` list is ``3`` in the
115114
``paint_purchases`` collection:
116115

117-
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
118-
:language: java
119-
:dedent:
120-
:start-after: begin arrayFilter
121-
:end-before: end arrayFilter
116+
.. io-code-block::
122117

123-
The following shows the output of the preceding query:
118+
.. input:: /examples/generated/QueryDocumentTest.snippet.array-filter.kt
119+
:language: kotlin
124120

125-
.. code-block:: json
126-
:copyable: false
121+
.. output::
122+
:language: console
127123

128-
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
129-
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }
124+
PaintOrder(id=2, qty=8, color=purple, vendor=[B, D, F], rating=5)
125+
PaintOrder(id=8, qty=7, color=black, vendor=[A, C, D], rating=null)
130126

131127
.. _query-elements:
132128

@@ -138,20 +134,17 @@ Element operators query data based on the presence or type of a field.
138134
The following example uses the ``Filters.exists()`` method to match
139135
documents that have a ``rating`` in the ``paint_purchases`` collection:
140136

141-
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
142-
:language: java
143-
:dedent:
144-
:start-after: begin elementFilter
145-
:end-before: end elementFilter
137+
.. io-code-block::
146138

147-
The following shows the output of the preceding query:
139+
.. input:: /examples/generated/QueryDocumentTest.snippet.element-filter.kt
140+
:language: kotlin
148141

149-
.. code-block:: json
150-
:copyable: false
142+
.. output::
143+
:language: console
151144

152-
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
153-
{ "_id": 4, "color": "white", "qty": 6, "vendor": ["D"], "rating": 9 }
154-
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }
145+
PaintOrder(id=2, qty=8, color=purple, vendor=[B, D, F], rating=5)
146+
PaintOrder(id=4, qty=6, color=white, vendor=[D], rating=9)
147+
PaintOrder(id=7, qty=8, color=green, vendor=[C, E], rating=7)
155148

156149
.. _query-evaluation:
157150

@@ -165,19 +158,16 @@ The following example uses the ``Filters.regex()`` method to match
165158
documents that have a ``color`` ending with the letter ``"k"`` in the
166159
``paint_purchases`` collection:
167160

168-
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
169-
:language: java
170-
:dedent:
171-
:start-after: begin evaluationFilter
172-
:end-before: end evaluationFilter
161+
.. io-code-block::
173162

174-
The following shows the output of the preceding query:
163+
.. input:: /examples/generated/QueryDocumentTest.snippet.evaluation-filter.kt
164+
:language: kotlin
175165

176-
.. code-block:: json
177-
:copyable: false
166+
.. output::
167+
:language: console
178168

179-
{ "_id": 6, "color": "pink", "qty": 3, "vendor": ["C"] }
180-
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }
169+
PaintOrder(id=6, qty=3, color=pink, vendor=[C], rating=null)
170+
PaintOrder(id=8, qty=7, color=black, vendor=[A, C, D], rating=null)
181171

182172
For more information about the operators mentioned in this guide,
183173
see the following Server Manual Entries:

0 commit comments

Comments
 (0)