@@ -4,17 +4,21 @@ import com.mongodb.client.model.Accumulators
4
4
import com.mongodb.client.model.Aggregates
5
5
import com.mongodb.client.model.Filters
6
6
import com.mongodb.client.model.Projections
7
+ import com.mongodb.client.model.search.SearchOperator
8
+ import com.mongodb.client.model.search.SearchPath.fieldPath
7
9
import com.mongodb.kotlin.client.coroutine.MongoClient
8
10
import config.getConfig
9
11
import kotlinx.coroutines.flow.toList
10
12
import kotlinx.coroutines.runBlocking
11
13
import org.bson.Document
12
14
import org.bson.codecs.pojo.annotations.BsonId
15
+ import org.bson.conversions.Bson
13
16
import org.bson.json.JsonWriterSettings
14
17
import org.junit.jupiter.api.AfterAll
15
18
import org.junit.jupiter.api.BeforeAll
16
19
import org.junit.jupiter.api.Test
17
20
import kotlin.test.assertEquals
21
+ import kotlin.test.Ignore
18
22
19
23
20
24
class AggregationTest {
@@ -44,16 +48,102 @@ class AggregationTest {
44
48
fun beforeAll () {
45
49
runBlocking {
46
50
val restaurants = listOf (
47
- Restaurant (
" Sun Bakery Trattoria" ,
Restaurant .
Contact (
" 386-555-0189" ,
" [email protected] " ,
listOf (
- 74.0056649 ,
40.7452371 )),
4 ,
listOf (
" Pizza" ,
" Pasta" ,
" Italian" ,
" Coffee" ,
" Sandwiches" )),
48
- Restaurant (
" Blue Bagels Grill" ,
Restaurant .
Contact (
" 786-555-0102" ,
" [email protected] " ,
listOf (
- 73.92506 ,
40.8275556 )),
3 ,
listOf (
" Bagels" ,
" Cookies" ,
" Sandwiches" )),
49
- Restaurant (
" XYZ Bagels Restaurant" ,
Restaurant .
Contact (
" 435-555-0190" ,
" [email protected] " ,
listOf (
- 74.0707363 ,
40.59321569999999 )),
4 ,
listOf (
" Bagels" ,
" Sandwiches" ,
" Coffee" )),
50
- Restaurant (
" Hot Bakery Cafe" ,
Restaurant .
Contact (
" 264-555-0171" ,
" [email protected] " ,
listOf (
- 73.96485799999999 ,
40.761899 )),
4 ,
listOf (
" Bakery" ,
" Cafe" ,
" Coffee" ,
" Dessert" )),
51
- Restaurant (
" Green Feast Pizzeria" ,
Restaurant .
Contact (
" 840-555-0102" ,
" [email protected] " ,
listOf (
- 74.1220973 ,
40.6129407 )),
2 ,
listOf (
" Pizza" ,
" Italian" )),
52
- Restaurant (
" ZZZ Pasta Buffet" ,
Restaurant .
Contact (
" 769-555-0152" ,
" [email protected] " ,
listOf (
- 73.9446421 ,
40.7253944 )),
0 ,
listOf (
" Pasta" ,
" Italian" ,
" Buffet" ,
" Cafeteria" )),
53
- Restaurant (
" XYZ Coffee Bar" ,
Restaurant .
Contact (
" 644-555-0193" ,
" [email protected] " ,
listOf (
- 74.0166091 ,
40.6284767 )),
5 ,
listOf (
" Coffee" ,
" Cafe" ,
" Bakery" ,
" Chocolates" )),
54
- Restaurant (
" 456 Steak Restaurant" ,
Restaurant .
Contact (
" 990-555-0165" ,
" [email protected] " ,
listOf (
- 73.9365108 ,
40.8497077 )),
0 ,
listOf (
" Steak" ,
" Seafood" )),
55
- Restaurant (
" 456 Cookies Shop" ,
Restaurant .
Contact (
" 604-555-0149" ,
" [email protected] " ,
listOf (
- 73.8850023 ,
40.7494272 )),
4 ,
listOf (
" Bakery" ,
" Cookies" ,
" Cake" ,
" Coffee" )),
56
- Restaurant (
" XYZ Steak Buffet" ,
Restaurant .
Contact (
" 229-555-0197" ,
" [email protected] " ,
listOf (
- 73.9799932 ,
40.7660886 )),
3 ,
listOf (
" Steak" ,
" Salad" ,
" Chinese" ))
51
+ Restaurant (
52
+ " Sun Bakery Trattoria" ,
53
+ Restaurant .Contact (
54
+ " 386-555-0189" ,
55
+
56
+ listOf (- 74.0056649 , 40.7452371 )
57
+ ),
58
+ 4 ,
59
+ listOf (" Pizza" , " Pasta" , " Italian" , " Coffee" , " Sandwiches" )
60
+ ),
61
+ Restaurant (
62
+ " Blue Bagels Grill" ,
63
+ Restaurant .Contact (
64
+ " 786-555-0102" ,
65
+
66
+ listOf (- 73.92506 , 40.8275556 )
67
+ ),
68
+ 3 ,
69
+ listOf (" Bagels" , " Cookies" , " Sandwiches" )
70
+ ),
71
+ Restaurant (
72
+ " XYZ Bagels Restaurant" ,
73
+ Restaurant .Contact (
74
+ " 435-555-0190" ,
75
+
76
+ listOf (- 74.0707363 , 40.59321569999999 )
77
+ ),
78
+ 4 ,
79
+ listOf (" Bagels" , " Sandwiches" , " Coffee" )
80
+ ),
81
+ Restaurant (
82
+ " Hot Bakery Cafe" ,
83
+ Restaurant .Contact (
84
+ " 264-555-0171" ,
85
+
86
+ listOf (- 73.96485799999999 , 40.761899 )
87
+ ),
88
+ 4 ,
89
+ listOf (" Bakery" , " Cafe" , " Coffee" , " Dessert" )
90
+ ),
91
+ Restaurant (
92
+ " Green Feast Pizzeria" ,
93
+ Restaurant .Contact (
94
+ " 840-555-0102" ,
95
+
96
+ listOf (- 74.1220973 , 40.6129407 )
97
+ ),
98
+ 2 ,
99
+ listOf (" Pizza" , " Italian" )
100
+ ),
101
+ Restaurant (
102
+ " ZZZ Pasta Buffet" ,
103
+ Restaurant .Contact (
104
+ " 769-555-0152" ,
105
+
106
+ listOf (- 73.9446421 , 40.7253944 )
107
+ ),
108
+ 0 ,
109
+ listOf (" Pasta" , " Italian" , " Buffet" , " Cafeteria" )
110
+ ),
111
+ Restaurant (
112
+ " XYZ Coffee Bar" ,
113
+ Restaurant .
Contact (
" 644-555-0193" ,
" [email protected] " ,
listOf (
- 74.0166091 ,
40.6284767 )),
114
+ 5 ,
115
+ listOf (" Coffee" , " Cafe" , " Bakery" , " Chocolates" )
116
+ ),
117
+ Restaurant (
118
+ " 456 Steak Restaurant" ,
119
+ Restaurant .Contact (
120
+ " 990-555-0165" ,
121
+
122
+ listOf (- 73.9365108 , 40.8497077 )
123
+ ),
124
+ 0 ,
125
+ listOf (" Steak" , " Seafood" )
126
+ ),
127
+ Restaurant (
128
+ " 456 Cookies Shop" ,
129
+ Restaurant .Contact (
130
+ " 604-555-0149" ,
131
+
132
+ listOf (- 73.8850023 , 40.7494272 )
133
+ ),
134
+ 4 ,
135
+ listOf (" Bakery" , " Cookies" , " Cake" , " Coffee" )
136
+ ),
137
+ Restaurant (
138
+ " XYZ Steak Buffet" ,
139
+ Restaurant .Contact (
140
+ " 229-555-0197" ,
141
+
142
+ listOf (- 73.9799932 , 40.7660886 )
143
+ ),
144
+ 3 ,
145
+ listOf (" Steak" , " Salad" , " Chinese" )
146
+ )
57
147
)
58
148
collection.insertMany(restaurants)
59
149
}
@@ -71,15 +161,17 @@ class AggregationTest {
71
161
}
72
162
73
163
@Test
74
- fun basicAggregationTest () = runBlocking {
164
+ fun basicAggregationTest () = runBlocking {
75
165
// :snippet-start: basic-aggregation
76
166
data class Results (@BsonId val id : Int , val count : Int )
77
167
78
168
val resultsFlow = collection.aggregate<Results >(
79
169
listOf (
80
170
Aggregates .match(Filters .eq(Restaurant ::categories.name, " Bakery" )),
81
- Aggregates .group(" \$ ${Restaurant ::stars.name} " ,
82
- Accumulators .sum(" count" , 1 ))
171
+ Aggregates .group(
172
+ " \$ ${Restaurant ::stars.name} " ,
173
+ Accumulators .sum(" count" , 1 )
174
+ )
83
175
)
84
176
)
85
177
@@ -124,7 +216,7 @@ class AggregationTest {
124
216
@Test
125
217
fun explainAggregationTest () = runBlocking {
126
218
// :snippet-start: explain-aggregation
127
- data class Results (val name : String , val count : Int )
219
+ data class Results (val name : String , val count : Int )
128
220
129
221
val explanation = collection.aggregate<Results >(
130
222
listOf (
@@ -143,14 +235,45 @@ class AggregationTest {
143
235
@Test
144
236
fun buildDocumentsTipTest () {
145
237
val method1 =
146
- // :snippet-start: build-documents-tip
147
- Document (" \$ arrayElemAt" , listOf (" \$ categories" , 0 ))
238
+ // :snippet-start: build-documents-tip
239
+ Document (" \$ arrayElemAt" , listOf (" \$ categories" , 0 ))
148
240
// is equivalent to
149
241
val method2 = // :remove:
150
- Document .parse(" { \$ arrayElemAt: ['\$ categories', 0] }" )
242
+ Document .parse(" { \$ arrayElemAt: ['\$ categories', 0] }" )
151
243
// :snippet-end:
152
244
// assert to test equivalency
153
245
assertEquals(method1, method2)
154
246
155
247
}
248
+
249
+ /* NOTE: Test is not run by default. FTS requires the creation of a text index on the collection before running.
250
+ */
251
+ @Ignore
252
+ fun atlasSearchOperatorTest () = runBlocking {
253
+ val collection = mongoClient.getDatabase(" sample_mflix" ).getCollection<Document >(" movies" )
254
+ // :snippet-start: atlas-search-pipeline
255
+ val searchStage = Aggregates .search(
256
+ SearchOperator .compound()
257
+ .filter(
258
+ listOf (
259
+ SearchOperator .`in `(fieldPath(" genres" ), listOf (" Comedy" )),
260
+ SearchOperator .phrase(fieldPath(" fullplot" ), " new york" ),
261
+ SearchOperator .numberRange(fieldPath(" year" )).gtLt(1950 , 2000 ),
262
+ SearchOperator .wildcard(fieldPath(" title" ), " Love *" )
263
+ )
264
+ )
265
+ )
266
+
267
+ val projectStage = Aggregates .project(
268
+ Projections .include(" title" , " year" , " genres" , " cast" ))
269
+
270
+ val pipeline = listOf (searchStage, projectStage)
271
+ val resultsFlow = collection.aggregate(pipeline)
272
+
273
+ resultsFlow.collect { println (it) }
274
+ // :snippet-end:
275
+
276
+ val result = resultsFlow.toList()
277
+ assertEquals(2 , result.size)
278
+ }
156
279
}
0 commit comments