Skip to content

Commit fe68c26

Browse files
authored
DOCSP-30872: improve Sorts and Sorts builders pages (#113)
# Pull Request Info [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - https://jira.mongodb.org/browse/DOCSP-30872 Staging: - [sorts builders](https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/DOCSP-30872-improve-sortbuilder-data/fundamentals/builders/sort/) - [sorts crud](https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/DOCSP-30872-improve-sortbuilder-data/fundamentals/crud/read-operations/sort/) ## Self-Review Checklist - [x] Is this free of any warnings or errors in the RST? - [x] Did you run a spell-check? - [x] Did you run a grammar-check? - [x] Are all the links working?
1 parent 7c36250 commit fe68c26

18 files changed

+238
-242
lines changed

examples/src/test/kotlin/SortTest.kt

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,32 @@ internal class SortTest {
1919

2020
companion object {
2121
// :snippet-start: sort-data-model
22-
data class FoodOrder(
22+
data class Order(
2323
@BsonId val id: Int,
24-
val letter: String,
25-
val food: String
24+
val date: String,
25+
val orderTotal: Double,
26+
val description: String,
2627
)
2728
// :snippet-end:
2829

2930
val config = getConfig()
3031
val client = MongoClient.create(config.connectionUri)
31-
val database = client.getDatabase("cafe")
32-
val collection = database.getCollection<FoodOrder>("food_order")
32+
val database = client.getDatabase("testDB")
33+
val collection = database.getCollection<Order>("orders")
3334

3435
@BeforeAll
3536
@JvmStatic
3637
fun beforeAll() {
3738
runBlocking {
38-
val foodOrders = listOf(
39-
FoodOrder(1, "c", "coffee with milk"),
40-
FoodOrder(3, "a", "maple syrup"),
41-
FoodOrder(4, "b", "coffee with sugar"),
42-
FoodOrder(5, "a", "milk and cookies"),
43-
FoodOrder(2, "a", "donuts and coffee"),
44-
FoodOrder(6, "c", "maple donut")
39+
val orders = listOf(
40+
Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"),
41+
Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes"),
42+
Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"),
43+
Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"),
44+
Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"),
45+
Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin")
4546
)
46-
collection.insertMany(foodOrders)
47+
collection.insertMany(orders)
4748
}
4849
}
4950

@@ -60,39 +61,39 @@ internal class SortTest {
6061
@Test
6162
fun basicSortTest() = runBlocking {
6263
// :snippet-start: basic-sort
63-
collection.find().sort(Sorts.ascending("_id"))
64+
val resultsFlow = collection.find().sort(Sorts.ascending(Order::orderTotal.name))
6465
// :snippet-end:
6566
// Junit test for the above code
6667
val filter = Filters.empty()
6768
val expected = listOf(
68-
FoodOrder(1, "c", "coffee with milk"),
69-
FoodOrder(2, "a", "donuts and coffee"),
70-
FoodOrder(3, "a", "maple syrup"),
71-
FoodOrder(4, "b", "coffee with sugar"),
72-
FoodOrder(5, "a", "milk and cookies"),
73-
FoodOrder(6, "c", "maple donut")
74-
69+
Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"),
70+
Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"),
71+
Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"),
72+
Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"),
73+
Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"),
74+
Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes")
7575
)
76-
assertEquals(expected, collection.find(filter).sort((Sorts.ascending("_id"))).toList() )
76+
77+
assertEquals(expected, resultsFlow.toList() )
7778
}
7879

7980
@Test
8081
fun aggregationSortTest() = runBlocking {
8182
// :snippet-start: aggregation-sort
8283
val resultsFlow = collection.aggregate(listOf(
83-
Aggregates.sort(Sorts.ascending("_id"))
84+
Aggregates.sort(Sorts.ascending(Order::orderTotal.name))
8485
))
8586

8687
resultsFlow.collect { println(it) }
8788
// :snippet-end:
8889
// Junit test for the above code
8990
val expected = listOf(
90-
FoodOrder(1, "c", "coffee with milk"),
91-
FoodOrder(2, "a", "donuts and coffee"),
92-
FoodOrder(3, "a", "maple syrup"),
93-
FoodOrder(4, "b", "coffee with sugar"),
94-
FoodOrder(5, "a", "milk and cookies"),
95-
FoodOrder(6, "c", "maple donut")
91+
Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"),
92+
Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"),
93+
Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"),
94+
Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"),
95+
Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"),
96+
Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes")
9697
)
9798
assertEquals(expected, resultsFlow.toList())
9899
}
@@ -101,18 +102,18 @@ internal class SortTest {
101102
fun ascendingSortTest() = runBlocking {
102103
// :snippet-start: ascending-sort
103104
val resultsFlow = collection.find()
104-
.sort(Sorts.ascending("_id"))
105+
.sort(Sorts.ascending(Order::orderTotal.name))
105106

106107
resultsFlow.collect { println(it) }
107108
// :snippet-end:
108109
// Junit test for the above code
109110
val expected = listOf(
110-
FoodOrder(1, "c", "coffee with milk"),
111-
FoodOrder(2, "a", "donuts and coffee"),
112-
FoodOrder(3, "a", "maple syrup"),
113-
FoodOrder(4, "b", "coffee with sugar"),
114-
FoodOrder(5, "a", "milk and cookies"),
115-
FoodOrder(6, "c", "maple donut")
111+
Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"),
112+
Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"),
113+
Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"),
114+
Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"),
115+
Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"),
116+
Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes")
116117
)
117118
assertEquals(expected, resultsFlow.toList())
118119
}
@@ -121,49 +122,49 @@ internal class SortTest {
121122
fun descendingSortTest() = runBlocking {
122123
// :snippet-start: descending-sort
123124
val resultsFlow = collection.find()
124-
.sort(Sorts.descending("_id"))
125+
.sort(Sorts.descending(Order::orderTotal.name))
125126

126127
resultsFlow.collect { println(it) }
127128
// :snippet-end:
128129
// Junit test for the above code
129130
val expected = listOf(
130-
FoodOrder(6, "c", "maple donut"),
131-
FoodOrder(5, "a", "milk and cookies"),
132-
FoodOrder(4, "b", "coffee with sugar"),
133-
FoodOrder(3, "a", "maple syrup"),
134-
FoodOrder(2, "a", "donuts and coffee"),
135-
FoodOrder(1, "c", "coffee with milk")
136-
)
131+
Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes"),
132+
Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"),
133+
Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"),
134+
Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"),
135+
Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"),
136+
Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin")
137+
)
137138
assertEquals(expected, resultsFlow.toList())
138139
}
139140

140141
@Test
141142
fun handleTiesTest() = runBlocking {
142143
// :snippet-start: handle-ties-1
143-
collection.find().sort(Sorts.ascending(FoodOrder::letter.name))
144+
collection.find().sort(Sorts.ascending(Order::date.name))
144145
// :snippet-end:
145146
val results =
146147
// :snippet-start: handle-ties-2
147-
collection.find().sort(Sorts.ascending(FoodOrder::letter.name, "_id"))
148+
collection.find().sort(Sorts.ascending(Order::date.name, Order::orderTotal.name))
148149
// :snippet-end:
149150
// Junit test for the above code
150151
val filter = Filters.empty()
151152
val expected = listOf(
152-
FoodOrder(2, "a", "donuts and coffee"),
153-
FoodOrder(3, "a", "maple syrup"),
154-
FoodOrder(5, "a", "milk and cookies"),
155-
FoodOrder(4, "b", "coffee with sugar"),
156-
FoodOrder(1, "c", "coffee with milk"),
157-
FoodOrder(6, "c", "maple donut")
158-
)
153+
Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"),
154+
Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"),
155+
Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes"),
156+
Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"),
157+
Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"),
158+
Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake")
159+
)
159160
assertEquals(expected, results.toList() )
160161
}
161162

162163
@Test
163164
fun combineSortTest() = runBlocking {
164165
// :snippet-start: combine-sort
165166
val orderBySort = Sorts.orderBy(
166-
Sorts.descending(FoodOrder::letter.name), Sorts.ascending("_id")
167+
Sorts.descending(Order::date.name), Sorts.ascending(Order::orderTotal.name)
167168
)
168169
val results = collection.find().sort(orderBySort)
169170

@@ -172,48 +173,47 @@ internal class SortTest {
172173
// Junit test for the above code
173174
val filter = Filters.empty()
174175
val expected = listOf(
175-
FoodOrder(1, "c", "coffee with milk"),
176-
FoodOrder(6, "c", "maple donut"),
177-
FoodOrder(4, "b", "coffee with sugar"),
178-
FoodOrder(2, "a", "donuts and coffee"),
179-
FoodOrder(3, "a", "maple syrup"),
180-
FoodOrder(5, "a", "milk and cookies")
176+
Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"),
177+
Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"),
178+
Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"),
179+
Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"),
180+
Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes"),
181+
Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"),
181182
)
182183
assertEquals(expected, results.toList() )
183184
}
184185

185186
@Test
186187
fun textSearchTest() = runBlocking {
187188
// :snippet-start: food-order-score
188-
data class FoodOrderScore(
189+
data class OrderScore(
189190
@BsonId val id: Int,
190-
val letter: String,
191-
val food: String,
192-
val score: Double
191+
val description: String,
192+
val score: Double
193193
)
194194
// :snippet-end:
195195
// :snippet-start: text-search
196-
collection.createIndex(Indexes.text(FoodOrderScore::food.name))
196+
collection.createIndex(Indexes.text(Order::description.name))
197197
val metaTextScoreSort = Sorts.orderBy(
198-
Sorts.metaTextScore(FoodOrderScore::score.name),
198+
Sorts.metaTextScore(OrderScore::score.name),
199199
Sorts.descending("_id")
200200
)
201-
val metaTextScoreProj = Projections.metaTextScore(FoodOrderScore::score.name)
202-
val searchTerm = "maple donut"
201+
val metaTextScoreProj = Projections.metaTextScore(OrderScore::score.name)
202+
val searchTerm = "vanilla"
203203
val searchQuery = Filters.text(searchTerm)
204204

205-
val results = collection.find<FoodOrderScore>(searchQuery)
205+
val results = collection.find<OrderScore>(searchQuery)
206206
.projection(metaTextScoreProj)
207207
.sort(metaTextScoreSort)
208208

209209
results.collect { println(it) }
210210
// :snippet-end:
211211
// Junit test for the above code
212212
val expected = listOf(
213-
FoodOrderScore(6, "c", "maple donut",1.5),
214-
FoodOrderScore(3, "a", "maple syrup", 0.75),
215-
FoodOrderScore(2, "a", "donuts and coffee", 0.75),
216-
)
213+
OrderScore(3, "1 dozen vanilla cupcakes", 0.625),
214+
OrderScore(5, "one large vanilla and chocolate cake", 0.6),
215+
OrderScore(2, "two medium vanilla birthday cakes", 0.6)
216+
)
217217
assertEquals(expected, results.toList())
218218
}
219219
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
val resultsFlow = collection.aggregate(listOf(
2-
Aggregates.sort(Sorts.ascending("_id"))
2+
Aggregates.sort(Sorts.ascending(Order::orderTotal.name))
33
))
44

55
resultsFlow.collect { println(it) }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
val resultsFlow = collection.find()
2-
.sort(Sorts.ascending("_id"))
2+
.sort(Sorts.ascending(Order::orderTotal.name))
33

44
resultsFlow.collect { println(it) }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
collection.find().sort(Sorts.ascending("_id"))
1+
val resultsFlow = collection.find().sort(Sorts.ascending(Order::orderTotal.name))

source/examples/generated/SortTest.snippet.combine-sort.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
val orderBySort = Sorts.orderBy(
2-
Sorts.descending(FoodOrder::letter.name), Sorts.ascending("_id")
2+
Sorts.descending(Order::date.name), Sorts.ascending(Order::orderTotal.name)
33
)
44
val results = collection.find().sort(orderBySort)
55

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
val resultsFlow = collection.find()
2-
.sort(Sorts.descending("_id"))
2+
.sort(Sorts.descending(Order::orderTotal.name))
33

44
resultsFlow.collect { println(it) }
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
data class FoodOrderScore(
1+
data class OrderScore(
22
@BsonId val id: Int,
3-
val letter: String,
4-
val food: String,
5-
val score: Double
3+
val description: String,
4+
val score: Double
65
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
collection.find().sort(Sorts.ascending(FoodOrder::letter.name))
1+
collection.find().sort(Sorts.ascending(Order::date.name))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
collection.find().sort(Sorts.ascending(FoodOrder::letter.name, "_id"))
1+
collection.find().sort(Sorts.ascending(Order::date.name, Order::orderTotal.name))
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
data class FoodOrder(
1+
data class Order(
22
@BsonId val id: Int,
3-
val letter: String,
4-
val food: String
3+
val date: String,
4+
val orderTotal: Double,
5+
val description: String,
56
)

0 commit comments

Comments
 (0)