@@ -4,17 +4,21 @@ import com.mongodb.client.model.Accumulators
44import com.mongodb.client.model.Aggregates
55import com.mongodb.client.model.Filters
66import com.mongodb.client.model.Projections
7+ import com.mongodb.client.model.search.SearchOperator
8+ import com.mongodb.client.model.search.SearchPath.fieldPath
79import com.mongodb.kotlin.client.coroutine.MongoClient
810import config.getConfig
911import kotlinx.coroutines.flow.toList
1012import kotlinx.coroutines.runBlocking
1113import org.bson.Document
1214import org.bson.codecs.pojo.annotations.BsonId
15+ import org.bson.conversions.Bson
1316import org.bson.json.JsonWriterSettings
1417import org.junit.jupiter.api.AfterAll
1518import org.junit.jupiter.api.BeforeAll
1619import org.junit.jupiter.api.Test
1720import kotlin.test.assertEquals
21+ import kotlin.test.Ignore
1822
1923
2024class AggregationTest {
@@ -44,16 +48,102 @@ class AggregationTest {
4448 fun beforeAll () {
4549 runBlocking {
4650 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+ )
57147 )
58148 collection.insertMany(restaurants)
59149 }
@@ -71,15 +161,17 @@ class AggregationTest {
71161 }
72162
73163 @Test
74- fun basicAggregationTest () = runBlocking {
164+ fun basicAggregationTest () = runBlocking {
75165 // :snippet-start: basic-aggregation
76166 data class Results (@BsonId val id : Int , val count : Int )
77167
78168 val resultsFlow = collection.aggregate<Results >(
79169 listOf (
80170 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+ )
83175 )
84176 )
85177
@@ -124,7 +216,7 @@ class AggregationTest {
124216 @Test
125217 fun explainAggregationTest () = runBlocking {
126218 // :snippet-start: explain-aggregation
127- data class Results (val name : String , val count : Int )
219+ data class Results (val name : String , val count : Int )
128220
129221 val explanation = collection.aggregate<Results >(
130222 listOf (
@@ -143,11 +235,11 @@ class AggregationTest {
143235 @Test
144236 fun buildDocumentsTipTest () {
145237 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 ))
148240 // is equivalent to
149241 val method2 = // :remove:
150- Document .parse(" { \$ arrayElemAt: ['\$ categories', 0] }" )
242+ Document .parse(" { \$ arrayElemAt: ['\$ categories', 0] }" )
151243 // :snippet-end:
152244 // assert to test equivalency
153245 assertEquals(method1, method2)
0 commit comments