1
1
import com.mongodb.client.model.Aggregates.limit
2
2
import com.mongodb.client.model.Aggregates.project
3
- import com.mongodb.client.model.Filters.eq
4
3
import com.mongodb.client.model.Projections.*
5
4
import com.mongodb.kotlin.client.coroutine.MongoClient
6
5
import kotlinx.coroutines.runBlocking
@@ -10,60 +9,66 @@ fun main() {
10
9
val uri = " <connection string>"
11
10
val mongoClient = MongoClient .create(uri)
12
11
13
- val database = mongoClient.getDatabase(" local_school_district " )
14
- val collection = database.getCollection<Document >(" schools " )
12
+ val database = mongoClient.getDatabase(" sample_mflix " )
13
+ val collection = database.getCollection<Document >(" movies " )
15
14
16
15
runBlocking {
17
- val agg = Document (" index" , " compound-query-custom-score-tutorial" )
18
- .append(
19
- " must" , listOf (
16
+ val agg = Document (
17
+ " \$ search" ,
18
+ Document (" index" , " compound-query-custom-score-tutorial" )
19
+ .append(
20
+ " compound" ,
20
21
Document (
21
- " text" ,
22
- Document (" path" , " genres" )
23
- .append(" query" , " comedy" )
24
- .append(
25
- " score" ,
26
- Document (
27
- " boost" ,
28
- Document (" value" , 9 )
29
- )
30
- )
31
- ),
32
- Document (
33
- " text" ,
34
- Document (" path" , " title" )
35
- .append(" query" , " snow" )
36
- .append(
37
- " score" ,
38
- Document (
39
- " boost" ,
40
- Document (" value" , 5 )
41
- )
22
+ " must" , listOf (
23
+ Document (
24
+ " text" ,
25
+ Document (" path" , " genres" )
26
+ .append(" query" , " comedy" )
27
+ .append(
28
+ " score" ,
29
+ Document (
30
+ " boost" ,
31
+ Document (" value" , 9 )
32
+ )
33
+ )
34
+ ),
35
+ Document (
36
+ " text" ,
37
+ Document (" path" , " title" )
38
+ .append(" query" , " snow" )
39
+ .append(
40
+ " score" ,
41
+ Document (
42
+ " boost" ,
43
+ Document (" value" , 5 )
44
+ )
45
+ )
42
46
)
47
+ )
43
48
)
44
- )
45
- )
46
- .append(
47
- " should" , listOf (
48
- Document (
49
- " range" ,
50
- Document (" path" , " year" )
51
- .append(" gte" , 2013 )
52
- .append(" lte" , 2015 )
53
- .append(
54
- " score" ,
49
+ .append(
50
+ " should" , listOf (
55
51
Document (
56
- " boost" ,
57
- Document (" value" , 3 )
52
+ " range" ,
53
+ Document (" path" , " year" )
54
+ .append(" gte" , 2013 )
55
+ .append(" lte" , 2015 )
56
+ .append(
57
+ " score" ,
58
+ Document (
59
+ " boost" ,
60
+ Document (" value" , 3 )
61
+ )
62
+ )
58
63
)
59
64
)
60
- )
65
+ )
61
66
)
62
- )
67
+ )
63
68
64
69
val resultsFlow = collection.aggregate<Document >(
65
70
listOf (
66
- eq( " \$ search " , eq( " compound " , agg)) ,
71
+ agg,
67
72
limit(10 ),
68
73
project(fields(
69
74
excludeId(),
0 commit comments