Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions source/examples/atlas-examples/AutocompleteQueryAdvanced.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import com.mongodb.client.model.Aggregates.limit
import com.mongodb.client.model.Aggregates.project
import com.mongodb.client.model.Projections.*
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import org.bson.Document

fun main() {
val uri = "<connection string>"

val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("sample_mflix")
val collection = database.getCollection<Document>("movies")

runBlocking {
val agg = Document(
"\$search",
Document("index", "autocomplete-tutorial")
.append(
"compound",
Document(
"should", listOf(
Document(
"autocomplete",
Document("query", "pri")
.append("path", "title")
),
Document(
"autocomplete",
Document("query", "pri")
.append("path", "plot")
)
)
)
.append("minimumShouldMatch", 1L)
)
)

val resultsFlow = collection.aggregate<Document>(
listOf(
agg,
limit(5),
project(fields(excludeId(), include("title")))
)
)

resultsFlow.collect { println(it) }
}

mongoClient.close()
}

36 changes: 36 additions & 0 deletions source/examples/atlas-examples/AutocompleteQueryBasic.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import com.mongodb.client.model.Aggregates.limit
import com.mongodb.client.model.Aggregates.project
import com.mongodb.client.model.Filters.eq
import com.mongodb.client.model.Projections.*
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import org.bson.Document

fun main() {
val uri = "<connection string>"

val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("sample_mflix")
val collection = database.getCollection<Document>("movies")

runBlocking {
val agg = Document(
"\$search",
Document("index", "autocomplete-tutorial")
.append("autocomplete", Document("query", "ger").append("path", "title"))
)

val resultsFlow = collection.aggregate<Document>(
listOf(
agg,
limit(20),
project(fields(excludeId(), include("title")))
)
)

resultsFlow.collect { println(it) }
}

mongoClient.close()
}

66 changes: 66 additions & 0 deletions source/examples/atlas-examples/BasicEmbeddedDocumentsSearch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import com.mongodb.client.model.Aggregates.limit
import com.mongodb.client.model.Aggregates.project
import com.mongodb.client.model.Projections.*
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import org.bson.Document

fun main() {
val uri = "<connection string>"

val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("local_school_district")
val collection = database.getCollection<Document>("schools")

runBlocking {
val mustClauses = listOf(
Document(
"text",
Document("path", "teachers.first").append("query", "John")
)
)

val shouldClauses = listOf(
Document(
"text",
Document("path", "teachers.last")
.append("query", "Smith")
)
)

val agg = Document(
"\$search", Document("index", "embedded-documents-tutorial")
.append(
"embeddedDocument",
Document("path", "teachers")
.append(
"operator",
Document(
"compound",
Document("must", mustClauses)
.append("should", shouldClauses)
)
)
)
.append("highlight", Document("path", "teachers.last"))
)

val resultsFlow = collection.aggregate<Document>(
listOf(
agg,
limit(5),
project(fields(
excludeId(),
include("teachers"),
computed("score", Document("\$meta", "searchScore")),
computed("highlights", Document("\$meta", "searchHighlights"))
))
)
)

resultsFlow.collect { println(it) }
}

mongoClient.close()
}

49 changes: 49 additions & 0 deletions source/examples/atlas-examples/ComplexEmbeddedDocumentQuery.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import com.mongodb.client.model.Aggregates.limit
import com.mongodb.client.model.Aggregates.project
import com.mongodb.client.model.Projections.*
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import org.bson.Document

fun main() {
val uri = "<connection string>"

val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("local_school_district")
val collection = database.getCollection<Document>("schools")

runBlocking {
val agg = Document(
"\$search",
Document("index", "embedded-documents-tutorial")
.append("embeddedDocument", Document("path", "clubs.sports")
.append(
"operator",
Document(
"queryString",
Document("defaultPath", "clubs.sports.club_name")
.append("query", "dodgeball OR frisbee")
)
)
)
)

val resultsFlow = collection.aggregate<Document>(
listOf(
agg,
limit(5),
project(
fields(
include("name", "clubs.sports"),
computed("score", Document("\$meta", "searchScore"))
)
)
)
)

resultsFlow.collect { println(it) }
}

mongoClient.close()
}

62 changes: 62 additions & 0 deletions source/examples/atlas-examples/CompoundBoostQuery.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import com.mongodb.client.model.Aggregates.limit
import com.mongodb.client.model.Aggregates.project
import com.mongodb.client.model.Projections.*
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import org.bson.Document

fun main() {
val uri = "<connection string>"

val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("sample_mflix")
val collection = database.getCollection<Document>("movies")

runBlocking {
val mustClauses = listOf(
Document(
"range", Document("path", "year")
.append("gte", 2013)
.append("lte", 2015)
)
)

val shouldClauses = listOf(
Document(
"text",
Document("query", "snow")
.append("path", "title")
.append("score", Document("boost", Document("value", 2)))
)
)

val highlightOption = Document("path", "title")

val agg = Document(
"\$search",
Document("index", "compound-query-custom-score-tutorial")
.append(
"compound",
Document("must", mustClauses).append("should", shouldClauses)
)
.append("highlight", highlightOption)
)

val resultsFlow = collection.aggregate<Document>(
listOf(
agg,
limit(10),
project(fields(
excludeId(),
include("title", "year"),
computed("score", Document("\$meta", "searchScore")),
computed("highlights", Document("\$meta", "searchHighlights"))
))
)
)

resultsFlow.collect { println(it) }
}

mongoClient.close()
}
63 changes: 63 additions & 0 deletions source/examples/atlas-examples/CompoundConstantQuery.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import com.mongodb.client.model.Aggregates.limit
import com.mongodb.client.model.Aggregates.project
import com.mongodb.client.model.Projections.*
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import org.bson.Document

fun main() {
val uri = "<connection string>"

val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("sample_mflix")
val collection = database.getCollection<Document>("movies")

runBlocking {
val mustClauses = listOf(
Document(
"range", Document("path", "year")
.append("gte", 2013)
.append("lte", 2015)
)
)

val shouldClauses = listOf(
Document(
"text",
Document("query", "snow")
.append("path", "title")
.append("score", Document("constant", Document("value", 5)))
)
)

val highlightOption = Document("path", "title")

val agg = Document(
"\$search",
Document("index", "compound-query-custom-score-tutorial")
.append(
"compound",
Document("must", mustClauses).append("should", shouldClauses)
)
.append("highlight", highlightOption)
)

val resultsFlow = collection.aggregate<Document>(
listOf(
agg,
limit(10),
project(fields(
excludeId(),
include("title", "year"),
computed("score", Document("\$meta", "searchScore")),
computed("highlights", Document("\$meta", "searchHighlights"))
))
)
)

resultsFlow.collect { println(it) }
}

mongoClient.close()
}

Loading