Skip to content

Commit 6f0ea6c

Browse files
authored
Merge pull request #25 from mongodben/DOCSP-29165
(DOCSP-29165): Kotlin Retrieve Data page
2 parents 29198f6 + 84cec34 commit 6f0ea6c

File tree

5 files changed

+141
-25
lines changed

5 files changed

+141
-25
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
import com.mongodb.client.model.Accumulators
3+
import com.mongodb.client.model.Aggregates
4+
import com.mongodb.client.model.Filters
5+
import com.mongodb.client.model.Sorts
6+
import com.mongodb.kotlin.client.coroutine.MongoClient
7+
import io.github.cdimascio.dotenv.dotenv
8+
import kotlinx.coroutines.flow.toList
9+
import kotlinx.coroutines.runBlocking
10+
import org.bson.codecs.pojo.annotations.BsonId
11+
import org.junit.jupiter.api.AfterAll
12+
import org.junit.jupiter.api.Assertions.*
13+
import org.junit.jupiter.api.BeforeAll
14+
import org.junit.jupiter.api.TestInstance
15+
import java.util.*
16+
import kotlin.test.*
17+
18+
// :snippet-start: retrieve-data-model
19+
data class PaintOrder(
20+
@BsonId val id: Int,
21+
val qty: Int,
22+
val color: String
23+
)
24+
// :snippet-end:
25+
26+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
27+
internal class RetrieveDataTest {
28+
29+
companion object {
30+
private val dotenv = dotenv()
31+
private val client = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"])
32+
private val database = client.getDatabase("paint_store")
33+
val collection = database.getCollection<PaintOrder>("paint_order")
34+
35+
@BeforeAll
36+
@JvmStatic
37+
fun beforeAll() {
38+
runBlocking {
39+
val paintOrders = listOf(
40+
PaintOrder(1, 10, "purple"),
41+
PaintOrder(2, 8, "green"),
42+
PaintOrder(3, 4, "purple"),
43+
PaintOrder(4, 11, "green")
44+
)
45+
collection.insertMany(paintOrders)
46+
}
47+
}
48+
49+
@AfterAll
50+
@JvmStatic
51+
fun afterAll() {
52+
runBlocking {
53+
collection.drop()
54+
client.close()
55+
}
56+
}
57+
}
58+
59+
@Test
60+
fun basicFindTest() = runBlocking {
61+
// :snippet-start: basic-find
62+
val filter = Filters.and(Filters.gt("qty", 3), Filters.lt("qty", 9))
63+
val resultsFlow = collection.find(filter)
64+
resultsFlow.collect { println(it) }
65+
// :snippet-end:
66+
val expected = listOf(
67+
PaintOrder(2, 8, "green"),
68+
PaintOrder(3, 4, "purple"),
69+
)
70+
assertEquals(expected, resultsFlow.toList())
71+
}
72+
73+
@Test
74+
fun aggregationFindTest() = runBlocking {
75+
// :snippet-start: aggregation-find
76+
data class AggregationResult(@BsonId val id: String, val qty: Int)
77+
78+
val filter = Filters.empty()
79+
val pipeline = listOf(
80+
Aggregates.match(filter),
81+
Aggregates.group(
82+
"\$color",
83+
Accumulators.sum("qty", "\$qty")
84+
),
85+
Aggregates.sort(Sorts.descending("qty"))
86+
)
87+
val resultsFlow = collection.aggregate<AggregationResult>(pipeline)
88+
resultsFlow.collect { println(it) }
89+
// :snippet-end:
90+
val expected = listOf(
91+
AggregationResult("green", 19),
92+
AggregationResult("purple", 14)
93+
)
94+
assertEquals(expected, resultsFlow.toList())
95+
}
96+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
data class AggregationResult(@BsonId val id: String, val qty: Int)
2+
3+
val filter = Filters.empty()
4+
val pipeline = listOf(
5+
Aggregates.match(filter),
6+
Aggregates.group(
7+
"\$color",
8+
Accumulators.sum("qty", "\$qty")
9+
),
10+
Aggregates.sort(Sorts.descending("qty"))
11+
)
12+
val resultsFlow = collection.aggregate<AggregationResult>(pipeline)
13+
resultsFlow.collect { println(it) }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val filter = Filters.and(Filters.gt("qty", 3), Filters.lt("qty", 9))
2+
val resultsFlow = collection.find(filter)
3+
resultsFlow.collect { println(it) }
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: Int,
4+
val color: String
5+
)

source/fundamentals/crud/read-operations/retrieve.txt

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ track of the color and quantity, which corresponds to the ``color`` and
4141
{ "_id": 3, "color": "purple", "qty": 4 }
4242
{ "_id": 4, "color": "green", "qty": 11 }
4343

44+
This data is modeled with the following Kotlin data class:
45+
46+
.. literalinclude:: /examples/generated/RetrieveDataTest.snippet.retrieve-data-model.kt
47+
:language: kotlin
48+
4449
.. _retrieve-find:
4550

4651
Find Operation
@@ -64,26 +69,23 @@ less than nine cans of paint from their :ref:`paint_order collection <retrieve-p
6469

6570
To address this scenario, the owner finds orders to match the criteria:
6671

67-
.. literalinclude:: /includes/fundamentals/code-snippets/Retrieve.java
68-
:language: java
69-
:dedent:
70-
:start-after: begin findExample
71-
:end-before: end findExample
72-
73-
For more information on how to build filters, see our :doc:`Filters Builders
74-
</fundamentals/builders/filters>` guide.
72+
.. io-code-block::
7573

76-
The following shows the output of the preceding query:
74+
.. input:: /examples/generated/RetrieveDataTest.snippet.basic-find.kt
75+
:language: kotlin
7776

78-
.. code-block:: json
79-
:copyable: false
77+
.. output::
78+
:language: console
8079

81-
{ "_id": 2, "color": "green", "qty": 8 }
82-
{ "_id": 3, "color": "purple", "qty": 4 }
80+
PaintOrder(id=2, qty=8, color=green)
81+
PaintOrder(id=3, qty=4, color=purple)
8382

8483
After the owner runs this query, they find two orders that matched the
8584
criteria.
8685

86+
For more information on how to build filters, see our :doc:`Filters Builders
87+
</fundamentals/builders/filters>` guide.
88+
8789
For a runnable ``find()`` example, see our :doc:`Find Multiple
8890
Documents </usage-examples/find>` page.
8991

@@ -116,19 +118,16 @@ To address the scenario, the owner creates an aggregation pipeline that:
116118
- Sums up the quantity field by color
117119
- Orders the results by highest-to-lowest quantity
118120

119-
.. literalinclude:: /includes/fundamentals/code-snippets/Retrieve.java
120-
:language: java
121-
:dedent:
122-
:start-after: begin aggregateExample
123-
:end-before: end aggregateExample
121+
.. io-code-block::
124122

125-
The following shows the output of the preceding aggregation:
123+
.. input:: /examples/generated/RetrieveDataTest.snippet.aggregation-find.kt
124+
:language: kotlin
126125

127-
.. code-block:: json
128-
:copyable: false
126+
.. output::
127+
:language: console
129128

130-
{ "_id": "green", "qty": 19 }
131-
{ "_id": "purple", "qty": 14 }
129+
PaintOrder(id=2, qty=8, color=green)
130+
PaintOrder(id=3, qty=4, color=purple)
132131

133132
After the owner runs the aggregation, they find that "green" is the most
134133
purchased color.
@@ -139,5 +138,5 @@ the MongoDB server manual page on :manual:`Aggregation </aggregation>`.
139138
For additional information on the methods mentioned on this page, see
140139
the following API Documentation:
141140

142-
- `MongoCollection.find() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#find()>`__
143-
- `MongoCollection.aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
141+
- `MongoCollection.find() <TODO:(DOCSP-29169)>`__
142+
- `MongoCollection.aggregate() <TODO:(DOCSP-29169)>`__

0 commit comments

Comments
 (0)