File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
source/examples/atlas-examples Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ import com.mongodb.client.model.Aggregates.limit
2
+ import com.mongodb.client.model.Aggregates.project
3
+ import com.mongodb.client.model.Projections.*
4
+ import com.mongodb.kotlin.client.coroutine.MongoClient
5
+ import kotlinx.coroutines.runBlocking
6
+ import org.bson.Document
7
+
8
+ fun main () {
9
+ val uri = " <connection-string>"
10
+ val mongoClient = MongoClient .create(uri)
11
+ val database = mongoClient.getDatabase(" sample_mflix" )
12
+ val collection = database.getCollection<Document >(" movies" )
13
+
14
+ runBlocking {
15
+ val agg = Document (
16
+ " \$ search" ,
17
+ Document (" index" , " case-insensitive-sort" )
18
+ .append(
19
+ " text" ,
20
+ Document (" path" , " title" )
21
+ .append(" query" , " train" )
22
+ )
23
+ .append(
24
+ " sort" ,
25
+ Document (" title" , 1 )
26
+ )
27
+ )
28
+
29
+ val resultsFlow = collection.aggregate<Document >(
30
+ listOf (
31
+ agg,
32
+ limit(5 ),
33
+ project(fields(
34
+ excludeId(),
35
+ include(" title" , " awards" ),
36
+ computed(" score" , Document (" \$ meta" , " searchScore" ))
37
+ ))
38
+ )
39
+ )
40
+
41
+ resultsFlow.collect { println (it) }
42
+ }
43
+
44
+ mongoClient.close()
45
+ }
46
+
You can’t perform that action at this time.
0 commit comments