1
1
2
2
import com.mongodb.MongoNamespace
3
- import com.mongodb.client.model.*
4
- import com.mongodb.client.model.Accumulators.*
5
- import com.mongodb.client.model.Aggregates.*
6
- import com.mongodb.client.model.Sorts.ascending
3
+ import com.mongodb.client.model.Accumulators
4
+ import com.mongodb.client.model.Aggregates
5
+ import com.mongodb.client.model.BucketAutoOptions
6
+ import com.mongodb.client.model.BucketGranularity
7
+ import com.mongodb.client.model.BucketOptions
8
+ import com.mongodb.client.model.Facet
9
+ import com.mongodb.client.model.Field
10
+ import com.mongodb.client.model.Filters
11
+ import com.mongodb.client.model.GraphLookupOptions
12
+ import com.mongodb.client.model.IndexOptions
13
+ import com.mongodb.client.model.Indexes
14
+ import com.mongodb.client.model.MergeOptions
15
+ import com.mongodb.client.model.MongoTimeUnit
16
+ import com.mongodb.client.model.Projections
17
+ import com.mongodb.client.model.Sorts
18
+ import com.mongodb.client.model.UnwindOptions
19
+ import com.mongodb.client.model.Variable
20
+ import com.mongodb.client.model.WindowOutputFields
21
+ import com.mongodb.client.model.Windows
7
22
import com.mongodb.client.model.densify.DensifyOptions
8
23
import com.mongodb.client.model.densify.DensifyRange
9
24
import com.mongodb.client.model.fill.FillOptions
@@ -21,7 +36,10 @@ import kotlinx.coroutines.runBlocking
21
36
import org.bson.Document
22
37
import org.bson.codecs.pojo.annotations.BsonId
23
38
import org.bson.types.ObjectId
24
- import org.junit.jupiter.api.*
39
+ import org.junit.jupiter.api.AfterAll
40
+ import org.junit.jupiter.api.BeforeAll
41
+ import org.junit.jupiter.api.Test
42
+ import org.junit.jupiter.api.TestInstance
25
43
import java.time.LocalDateTime
26
44
import kotlin.test.Ignore
27
45
import kotlin.test.assertEquals
@@ -116,8 +134,8 @@ class AggregatesBuilderTest {
116
134
val collection = database.getCollection<Document >(" someCollection" )
117
135
collection.insertOne(Document (" someField" , " someCriteria" ))
118
136
// :snippet-start: methods-example
119
- val matchStage = match(Filters .eq(" someField" , " someCriteria" ))
120
- val sortByCountStage = sortByCount(" \$ someField" )
137
+ val matchStage = Aggregates . match(Filters .eq(" someField" , " someCriteria" ))
138
+ val sortByCountStage = Aggregates . sortByCount(" \$ someField" )
121
139
val results = collection.aggregate(
122
140
listOf (matchStage, sortByCountStage)).toList()
123
141
// :snippet-end:
@@ -338,8 +356,8 @@ class AggregatesBuilderTest {
338
356
val grouping = orderCollection.aggregate<Results >(listOf (
339
357
// :snippet-start: group
340
358
Aggregates .group(" \$ ${Order ::customerId.name} " ,
341
- sum(" totalQuantity" , " \$ ${Order ::ordered.name} " ),
342
- avg(" averageQuantity" , " \$ ${Order ::ordered.name} " )
359
+ Accumulators . sum(" totalQuantity" , " \$ ${Order ::ordered.name} " ),
360
+ Accumulators . avg(" averageQuantity" , " \$ ${Order ::ordered.name} " )
343
361
)
344
362
// :snippet-end:
345
363
))
@@ -673,7 +691,7 @@ class AggregatesBuilderTest {
673
691
674
692
val resultsFlow = translateCollection.aggregate<Results >(listOf (
675
693
// :snippet-start: replace-root
676
- replaceRoot(" \$ ${Book ::spanishTranslation.name} " )
694
+ Aggregates . replaceRoot(" \$ ${Book ::spanishTranslation.name} " )
677
695
// :snippet-end:
678
696
679
697
))
@@ -686,7 +704,7 @@ class AggregatesBuilderTest {
686
704
data class Results (val watched : Boolean , val type : String )
687
705
val resultsFlow = movieCollection.aggregate<Results >(listOf (
688
706
// :snippet-start: add-fields
689
- addFields(
707
+ Aggregates . addFields(
690
708
Field (" watched" , false ),
691
709
Field (" type" , " movie" )
692
710
)
@@ -711,7 +729,7 @@ class AggregatesBuilderTest {
711
729
fun bucketTest () = runBlocking {
712
730
val resultsFlow = screenCollection.aggregate<Document >(listOf (
713
731
// :snippet-start: bucket
714
- bucket(" \$ ${Screen ::screenSize.name} " , listOf (0 , 24 , 32 , 50 , 70 , 1000 ))
732
+ Aggregates . bucket(" \$ ${Screen ::screenSize.name} " , listOf (0 , 24 , 32 , 50 , 70 , 1000 ))
715
733
// :snippet-end:
716
734
))
717
735
assertEquals(4 , resultsFlow.toList().size)
@@ -722,12 +740,12 @@ class AggregatesBuilderTest {
722
740
data class Results (val count : Int , val matches : List <Int >)
723
741
val resultsFlow = screenCollection.aggregate<Results >(listOf (
724
742
// :snippet-start: bucket-options
725
- bucket(" \$ ${Screen ::screenSize.name} " , listOf (0 , 24 , 32 , 50 , 70 ),
743
+ Aggregates . bucket(" \$ ${Screen ::screenSize.name} " , listOf (0 , 24 , 32 , 50 , 70 ),
726
744
BucketOptions ()
727
745
.defaultBucket(" monster" )
728
746
.output(
729
- sum(" count" , 1 ),
730
- push(" matches" , " \$ ${Screen ::screenSize.name} " )
747
+ Accumulators . sum(" count" , 1 ),
748
+ Accumulators . push(" matches" , " \$ ${Screen ::screenSize.name} " )
731
749
)
732
750
)
733
751
// :snippet-end:
@@ -741,7 +759,7 @@ class AggregatesBuilderTest {
741
759
data class Results (@BsonId val id : MinMax , val count : Int )
742
760
val resultsFlow = screenCollection.aggregate<Results >(listOf (
743
761
// :snippet-start: bucket-auto
744
- bucketAuto(" \$ ${Screen ::screenSize.name} " , 5 )
762
+ Aggregates . bucketAuto(" \$ ${Screen ::screenSize.name} " , 5 )
745
763
// :snippet-end:
746
764
))
747
765
assertEquals(5 , resultsFlow.toList().size)
@@ -755,11 +773,11 @@ class AggregatesBuilderTest {
755
773
// example kotlin nested data classes
756
774
val resultsFlow = screenCollection.aggregate<Results >(listOf (
757
775
// :snippet-start: bucket-auto-options
758
- bucketAuto(
776
+ Aggregates . bucketAuto(
759
777
" \$ ${Screen ::price.name} " , 5 ,
760
778
BucketAutoOptions ()
761
779
.granularity(BucketGranularity .POWERSOF2 )
762
- .output(sum(" count" , 1 ), avg(" avgPrice" , " \$ ${Screen ::price.name} " ))
780
+ .output(Accumulators . sum(" count" , 1 ), Accumulators . avg(" avgPrice" , " \$ ${Screen ::price.name} " ))
763
781
)
764
782
// :snippet-end:
765
783
))
@@ -774,19 +792,19 @@ class AggregatesBuilderTest {
774
792
data class Results (val `Screen Sizes `: List <ScreenSize >)
775
793
val resultsFlow = screenCollection.aggregate<Results >(listOf (
776
794
// :snippet-start: facet
777
- facet(
795
+ Aggregates . facet(
778
796
Facet (
779
797
" Screen Sizes" ,
780
- bucketAuto(
798
+ Aggregates . bucketAuto(
781
799
" \$ ${Screen ::screenSize.name} " ,
782
800
5 ,
783
- BucketAutoOptions ().output(sum(" count" , 1 ))
801
+ BucketAutoOptions ().output(Accumulators . sum(" count" , 1 ))
784
802
)
785
803
),
786
804
Facet (
787
805
" Manufacturer" ,
788
- sortByCount(" \$ ${Screen ::manufacturer.name} " ),
789
- limit(5 )
806
+ Aggregates . sortByCount(" \$ ${Screen ::manufacturer.name} " ),
807
+ Aggregates . limit(5 )
790
808
)
791
809
)
792
810
// :snippet-end:
@@ -852,7 +870,7 @@ class AggregatesBuilderTest {
852
870
val resultsFlow = weatherCollection.aggregate<Weather >(
853
871
listOf (
854
872
Aggregates .fill(
855
- FillOptions .fillOptions().sortBy(ascending(Weather ::hour.name)),
873
+ FillOptions .fillOptions().sortBy(Sorts . ascending(Weather ::hour.name)),
856
874
FillOutputField .value(Weather ::temperature.name, " 23.6C" ),
857
875
FillOutputField .linear(Weather ::air_pressure.name)
858
876
)
@@ -881,7 +899,7 @@ class AggregatesBuilderTest {
881
899
weatherCollection.insertMany(weather)
882
900
val resultsFlow = weatherCollection.aggregate<Document >(listOf (
883
901
// :snippet-start: densify
884
- densify(
902
+ Aggregates . densify(
885
903
" ts" ,
886
904
DensifyRange .partitionRangeWithStep(15 , MongoTimeUnit .MINUTE ),
887
905
DensifyOptions .densifyOptions().partitionByFields(" Position.coordinates" )
0 commit comments