Skip to content

Commit 7abfefa

Browse files
committed
add two lang example
1 parent 482806c commit 7abfefa

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import com.mongodb.client.model.Aggregates.project
2+
import com.mongodb.client.model.Projections.*
3+
import com.mongodb.kotlin.client.coroutine.MongoClient
4+
import kotlinx.coroutines.runBlocking
5+
import org.bson.Document
6+
import java.time.Instant
7+
import java.util.*
8+
9+
fun main() {
10+
val uri = "<connection string>"
11+
val mongoClient = MongoClient.create(uri)
12+
13+
val database = mongoClient.getDatabase("sample_mflix")
14+
val collection = database.getCollection<Document>("movies")
15+
16+
runBlocking {
17+
val mustClauses = listOf(
18+
Document(
19+
"text",
20+
Document("path", Document("value", "fullplot")
21+
.append("multi", "fullplot_english"))
22+
.append("query", "Bella")
23+
)
24+
)
25+
26+
val mustNotClauses = listOf(
27+
Document(
28+
"range", Document("path", "released")
29+
.append("gt", Date.from(Instant.parse("1984-01-01T00:00:00.000Z")))
30+
.append("lt", Date.from(Instant.parse("2016-01-01T00:00:00.000Z")))
31+
)
32+
)
33+
34+
val shouldClauses = listOf(
35+
Document(
36+
"text",
37+
Document("query", "Comedy")
38+
.append("path", "genres")
39+
)
40+
)
41+
42+
val agg = Document(
43+
"\$search", Document( "index", "multilingual-tutorial")
44+
.append("compound",
45+
Document().append("must", mustClauses)
46+
.append("mustNot", mustNotClauses)
47+
.append("should", shouldClauses)
48+
)
49+
)
50+
51+
val resultsFlow = collection.aggregate<Document>(
52+
listOf(
53+
agg,
54+
project(fields(
55+
excludeId(),
56+
include("title", "fullplot", "released", "genres"),
57+
computed("score", Document("\$meta", "searchScore"))
58+
))
59+
)
60+
)
61+
resultsFlow.collect { println(it) }
62+
}
63+
mongoClient.close()
64+
}

0 commit comments

Comments
 (0)