Skip to content

Commit fdb67ca

Browse files
committed
add case insensitive example
1 parent 9d38fea commit fdb67ca

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+

0 commit comments

Comments
 (0)