Skip to content

Commit 619f989

Browse files
authored
wip
1 parent d13b106 commit 619f989

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

source/includes/aggregation/aggregation.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import com.mongodb.client.model.Accumulators
44
import com.mongodb.client.model.Aggregates
55
import com.mongodb.client.model.Filters
66
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
710
import org.bson.Document
811

912
// start-data-class
@@ -44,25 +47,24 @@ fun main() {
4447
// end-aggregation-explain
4548

4649
// 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(
5053
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 *")
5658
)
59+
)
5760
)
5861

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)
6267

63-
val aggregatePipelineStages: List<Bson> = listOf(searchStage, Aggregates.project(projection))
64-
val results = collection.aggregate<Document>(aggregatePipelineStages)
65-
6668
results.forEach { result -> println(result) }
6769
// end-atlas-searchoperator-helpers
6870
}

0 commit comments

Comments
 (0)