@@ -4,6 +4,9 @@ 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.kotlin.client.MongoClient
7
+ import com.mongodb.client.model.Projections
8
+ import com.mongodb.client.model.search.SearchOperator
9
+ import com.mongodb.client.model.search.SearchPath.fieldPath
7
10
import org.bson.Document
8
11
9
12
// start-data-class
@@ -44,25 +47,24 @@ fun main() {
44
47
// end-aggregation-explain
45
48
46
49
// start-atlas-searchoperator-helpers
47
- val searchStage: Bson = Aggregates .search(
48
- SearchOperator .compound()
49
- .filter(
50
+ val searchStage = Aggregates .search(
51
+ SearchOperator .compound()
52
+ .filter(
50
53
listOf (
51
- SearchOperator .text(fieldPath(" genres" ), " Drama" ),
52
- SearchOperator .phrase(fieldPath(" cast" ), " sylvester stallone" ),
53
- SearchOperator .numberRange(fieldPath(" year" )).gtLt(1980 , 1989 ),
54
- SearchOperator .wildcard(fieldPath(" title" ), " Rocky *" )
55
- )
54
+ SearchOperator .text(fieldPath(" genres" ), " Drama" ),
55
+ SearchOperator .phrase(fieldPath(" cast" ), " sylvester stallone" ),
56
+ SearchOperator .numberRange(fieldPath(" year" )).gtLt(1980 , 1989 ),
57
+ SearchOperator .wildcard(fieldPath(" title" ), " Rocky *" )
56
58
)
59
+ )
57
60
)
58
61
59
- val projection = Projections .fields(
60
- Projections .include(" title" , " year" , " genres" , " cast" )
61
- )
62
+ val projectStage = Aggregates .project(
63
+ Projections .include(" title" , " year" , " genres" , " cast" ))
64
+
65
+ val aggregatePipelineStages = listOf (searchStage, projectStage)
66
+ val results = collection.aggregate(aggregatePipelineStages)
62
67
63
- val aggregatePipelineStages: List <Bson > = listOf (searchStage, Aggregates .project(projection))
64
- val results = collection.aggregate<Document >(aggregatePipelineStages)
65
-
66
68
results.forEach { result -> println (result) }
67
69
// end-atlas-searchoperator-helpers
68
70
}
0 commit comments