Skip to content

Commit 19b0326

Browse files
authored
(DOCSP-29213): CRUD > Skip Returned Results page (#14)
* (DOCSP-29213): Skip Returned Results page * Rename snippets * Optimize imports * Remove unnecessary import examples * Apply internal review feedback * io-code block * Convert to Flow.collect { } * Apply review feedback
1 parent 5484add commit 19b0326

File tree

8 files changed

+211
-35
lines changed

8 files changed

+211
-35
lines changed

examples/src/test/kotlin/SkipTest.kt

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
import com.mongodb.client.model.Aggregates
3+
import com.mongodb.client.model.Filters
4+
import com.mongodb.client.model.Sorts.descending
5+
import com.mongodb.kotlin.client.coroutine.MongoClient
6+
import io.github.cdimascio.dotenv.dotenv
7+
import kotlinx.coroutines.flow.toList
8+
import kotlinx.coroutines.runBlocking
9+
import org.bson.codecs.pojo.annotations.BsonId
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+
18+
// :snippet-start: skip-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 SkipTest {
28+
29+
companion object {
30+
val dotenv = dotenv()
31+
val client = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"])
32+
val database = client.getDatabase("paint_store")
33+
val collection = database.getCollection<PaintOrder>("paint_order")
34+
35+
@BeforeAll
36+
@JvmStatic
37+
private fun beforeAll() {
38+
runBlocking {
39+
40+
val paintOrders = listOf(
41+
PaintOrder(1, 5, "red"),
42+
PaintOrder(2, 10, "purple"),
43+
PaintOrder(3, 9, "blue"),
44+
PaintOrder(4, 6, "white"),
45+
PaintOrder(5, 11, "yellow"),
46+
PaintOrder(6, 3, "pink"),
47+
PaintOrder(7, 8, "green"),
48+
PaintOrder(8, 7, "orange")
49+
)
50+
collection.insertMany(paintOrders)
51+
}
52+
}
53+
54+
55+
@AfterAll
56+
@JvmStatic
57+
private fun afterAll() {
58+
runBlocking {
59+
collection.drop()
60+
client.close()
61+
}
62+
}
63+
}
64+
65+
@Test
66+
fun basicSkipTest() = runBlocking {
67+
// :snippet-start: basic-skip
68+
collection.find().skip(2)
69+
// :snippet-end:
70+
71+
// :snippet-start: aggregates-skip
72+
val filter = Filters.empty()
73+
val results = collection.aggregate(listOf(
74+
Aggregates.match(filter),
75+
Aggregates.skip(2))
76+
)
77+
// :snippet-end:
78+
79+
// Junit test for the above code
80+
val expected = listOf(
81+
PaintOrder(3, 9, "blue"),
82+
PaintOrder(4, 6, "white"),
83+
PaintOrder(5, 11, "yellow"),
84+
PaintOrder(6, 3, "pink"),
85+
PaintOrder(7, 8, "green"),
86+
PaintOrder(8, 7, "orange")
87+
)
88+
assertEquals(expected, results.toList())
89+
}
90+
91+
@Test
92+
fun findIterableTest() = runBlocking {
93+
// :snippet-start: find-iterable
94+
val filter = Filters.empty()
95+
val results = collection.find(filter)
96+
.sort(descending(PaintOrder::qty.name))
97+
.skip(5)
98+
.collect { println(it) }
99+
// :snippet-end:
100+
// Junit test for the above code
101+
val expected = listOf(
102+
PaintOrder(4, 6, "white"),
103+
PaintOrder(1, 5, "red"),
104+
PaintOrder(6, 3, "pink")
105+
)
106+
assertEquals(expected, collection.find(filter).sort(descending(PaintOrder::qty.name))
107+
.skip(5).toList() )
108+
}
109+
110+
@Test
111+
fun basicAggregationTest() = runBlocking {
112+
// :snippet-start: aggregation
113+
val filter = Filters.empty()
114+
val aggregate = listOf(
115+
Aggregates.match(filter),
116+
Aggregates.sort(descending(PaintOrder::qty.name)),
117+
Aggregates.skip(5)
118+
)
119+
collection.aggregate(aggregate).collect { println(it) }
120+
// :snippet-end:
121+
122+
// Junit test for the above code
123+
val expected = listOf(
124+
PaintOrder(4, 6, "white"),
125+
PaintOrder(1, 5, "red"),
126+
PaintOrder(6, 3, "pink")
127+
)
128+
assertEquals(expected, collection.aggregate(aggregate).toList())
129+
}
130+
131+
@Test
132+
fun noResultsTest() = runBlocking {
133+
// :snippet-start: no-results
134+
val filter = Filters.empty()
135+
val emptyQuery = listOf(
136+
Aggregates.match(filter),
137+
Aggregates.sort(descending(PaintOrder::qty.name)),
138+
Aggregates.skip(9)
139+
)
140+
collection.aggregate(emptyQuery).collect { println(it) }
141+
// :snippet-end:
142+
// Junit test for the above code
143+
assertTrue(collection.aggregate(emptyQuery).toList().isEmpty())
144+
}
145+
}
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 results = collection.aggregate(listOf(
3+
Aggregates.match(filter),
4+
Aggregates.skip(2))
5+
)
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 aggregate = listOf(
3+
Aggregates.match(filter),
4+
Aggregates.sort(descending(PaintOrder::qty.name)),
5+
Aggregates.skip(5)
6+
)
7+
collection.aggregate(aggregate).collect { println(it) }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
collection.find().skip(2)
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 results = collection.find(filter)
3+
.sort(descending(PaintOrder::qty.name))
4+
.skip(5)
5+
.collect { println(it) }
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 emptyQuery = listOf(
3+
Aggregates.match(filter),
4+
Aggregates.sort(descending(PaintOrder::qty.name)),
5+
Aggregates.skip(9)
6+
)
7+
collection.aggregate(emptyQuery).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/skip.txt

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,29 @@ Overview
1616
--------
1717

1818
In this guide, you can learn how to skip a specified number of returned
19-
results from read operations with the MongoDB Java driver.
19+
results from read operations with the MongoDB Kotlin driver.
2020

2121
You can skip results on the returned results of a query by using the
2222
``skip()`` method. You can also skip documents at a specific stage in an
2323
aggregation pipeline by specifying a ``$skip`` aggregation stage.
2424

2525
The ``skip()`` method takes an integer that specifies the number of documents
2626
to omit from the beginning of the list of documents returned by the
27-
`FindIterable <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html#skip(int)>`__.
27+
`FindIterable <TODO:(DOCSP-29169)>`__.
2828

2929
You can use the ``skip()`` method to skip the first two documents as follows:
3030

31-
.. code-block:: java
31+
.. literalinclude:: /examples/generated/SkipTest.snippet.basic-skip.kt
32+
:language: kotlin
3233

33-
collection.find().skip(2);
34-
35-
`Aggregates.skip() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#skip(int)>`__
34+
`Aggregates.skip() <TODO:(DOCSP-29169)>`__
3635
is an optional stage in the aggregation pipeline that specifies how many
3736
documents to omit from the beginning of the results of the prior stage.
3837

3938
You can use the ``Aggregates.skip()`` method to skip the first two documents as follows:
4039

41-
.. code-block:: java
42-
43-
import com.mongodb.client.model.Aggregates;
44-
45-
collection.aggregate(Arrays.asList(Aggregates.match(), Aggregates.skip(2)));
40+
.. literalinclude:: /examples/generated/SkipTest.snippet.aggregates-skip.kt
41+
:language: kotlin
4642

4743
Examples
4844
--------
@@ -64,6 +60,10 @@ field in their ``paint_inventory`` collection:
6460
{ "_id": 7, "color": "green", "qty": 8 }
6561
{ "_id": 8, "color": "orange", "qty": 7 }
6662

63+
This data is modeled with the following Kotlin data class:
64+
65+
.. literalinclude:: /examples/generated/SkipTest.snippet.skip-data-model.kt
66+
:language: kotlin
6767

6868
To address the scenario, the paint store needs to query the
6969
``paint_inventory`` collection with an empty filter, sort the documents
@@ -72,11 +72,17 @@ by ``qty`` field and omit the first five results.
7272
Using a FindIterable
7373
~~~~~~~~~~~~~~~~~~~~
7474

75-
.. literalinclude:: /includes/fundamentals/code-snippets/Skip.java
76-
:language: java
77-
:dedent:
78-
:start-after: begin skipExample
79-
:end-before: end skipExample
75+
.. io-code-block::
76+
77+
.. input:: /examples/generated/SkipTest.snippet.find-iterable.kt
78+
:language: kotlin
79+
80+
.. output::
81+
:language: console
82+
83+
PaintOrder(id=4, qty=6, color=white)
84+
PaintOrder(id=1, qty=5, color=red)
85+
PaintOrder(id=6, qty=3, color=pink)
8086

8187
- The ``find()`` method returns all documents.
8288
- The ``sort()`` method specifies documents to display from highest to lowest based on the ``qty`` field.
@@ -85,25 +91,22 @@ Using a FindIterable
8591
Using Aggregation
8692
~~~~~~~~~~~~~~~~~
8793

88-
.. literalinclude:: /includes/fundamentals/code-snippets/Skip.java
89-
:language: java
90-
:dedent:
91-
:start-after: begin skipAggregateExample
92-
:end-before: end skipAggregateExample
94+
.. io-code-block::
95+
96+
.. input:: /examples/generated/SkipTest.snippet.aggregation.kt
97+
:language: kotlin
98+
99+
.. output::
100+
:language: console
101+
102+
PaintOrder(id=4, qty=6, color=white)
103+
PaintOrder(id=1, qty=5, color=red)
104+
PaintOrder(id=6, qty=3, color=pink)
93105

94106
- The ``match()`` stage returns all documents.
95107
- The ``sort()`` stage specifies documents to display from highest to lowest based on the ``qty`` field.
96108
- The ``skip()`` stage specifies to omit the first five documents.
97109

98-
The following shows the output of both preceding queries:
99-
100-
.. code-block:: json
101-
:copyable: false
102-
103-
{ "_id": 4, "color": "white", "qty": 6 }
104-
{ "_id": 1, "color": "red", "qty": 5 }
105-
{ "_id": 6, "color": "pink", "qty": 3 }
106-
107110
After the paint store runs the query, they find the three best-selling colors are pink,
108111
red, and white.
109112

@@ -116,8 +119,6 @@ red, and white.
116119
documents, no results would return since the specified quantity
117120
exceeds the number of matched documents.
118121

119-
.. literalinclude:: /includes/fundamentals/code-snippets/Skip.java
120-
:language: java
121-
:dedent:
122-
:start-after: begin noResultsExample
123-
:end-before: end noResultsExample
122+
.. literalinclude:: /examples/generated/SkipTest.snippet.no-results.kt
123+
:language: kotlin
124+

0 commit comments

Comments
 (0)