Skip to content

Commit 11bd648

Browse files
committed
add lookup and union examples
1 parent 3c80609 commit 11bd648

File tree

4 files changed

+238
-1
lines changed

4 files changed

+238
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
import java.util.*
8+
9+
fun main() {
10+
val uri = "<connection string>"
11+
val mongoClient = MongoClient.create(uri)
12+
val database = mongoClient.getDatabase("sample_analytics")
13+
val collection = database.getCollection<Document>("customers")
14+
15+
runBlocking {
16+
val agg = Document(
17+
"\$lookup",
18+
Document("from", "accounts")
19+
.append("localField", "accounts")
20+
.append("foreignField", "account_id")
21+
.append("as", "purchases")
22+
.append(
23+
"pipeline", Arrays.asList(
24+
Document(
25+
"\$search",
26+
Document("index", "lookup-with-search-tutorial")
27+
.append(
28+
"compound",
29+
Document(
30+
"must", Arrays.asList(
31+
Document(
32+
"queryString",
33+
Document("defaultPath", "products")
34+
.append("query", "products: (CurrencyService AND InvestmentStock)")
35+
)
36+
)
37+
)
38+
.append(
39+
"should", Arrays.asList(
40+
Document(
41+
"range",
42+
Document("path", "limit")
43+
.append("gte", 5000)
44+
.append("lte", 10000)
45+
)
46+
)
47+
)
48+
)
49+
),
50+
Document("\$limit", 5),
51+
Document(
52+
"\$project",
53+
Document("_id", 0)
54+
.append("address", 0)
55+
.append("birthdate", 0)
56+
.append("username", 0)
57+
.append("tier_and_details", 0)
58+
)
59+
)
60+
)
61+
)
62+
63+
val resultsFlow = collection.aggregate<Document>(
64+
listOf(
65+
agg,
66+
limit(5),
67+
project(fields(excludeId(), include("name", "email", "active", "accounts", "purchases")))
68+
)
69+
)
70+
resultsFlow.collect { println(it) }
71+
}
72+
mongoClient.close()
73+
}
74+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import com.mongodb.kotlin.client.coroutine.MongoClient
2+
import kotlinx.coroutines.runBlocking
3+
import org.bson.Document
4+
5+
6+
fun main() {
7+
val uri = "<connection string>"
8+
val mongoClient = MongoClient.create(uri)
9+
val database = mongoClient.getDatabase("sample_training")
10+
val collection = database.getCollection<Document>("companies")
11+
12+
runBlocking {
13+
val pipeline1 = listOf(
14+
Document("\$search", Document("text",
15+
Document("query", "Mobile")
16+
.append("path", "name"))), Document("\$project", Document("score",
17+
Document("\$meta", "searchScore"))
18+
.append("_id", 0)
19+
.append("number_of_employees", 1)
20+
.append("founded_year", 1)
21+
.append("name", 1)), Document("\$set", Document("source", "companies")),
22+
Document("\$limit", 3)
23+
)
24+
25+
val pipeline2 = listOf(
26+
Document(
27+
"\$search", Document(
28+
"text",
29+
Document("query", "Mobile")
30+
.append("path", "business_name")
31+
)
32+
),
33+
Document("\$set", Document("source", "inspections")),
34+
Document(
35+
"\$project", Document(
36+
"score",
37+
Document("\$meta", "searchScore")
38+
)
39+
.append("source", 1)
40+
.append("_id", 0)
41+
.append("business_name", 1)
42+
.append("address", 1)
43+
),
44+
Document("\$limit", 3),
45+
Document("\$sort", Document("score", -1))
46+
)
47+
48+
val unionWithStage: MutableList<Document> = ArrayList()
49+
val unionWith = Document(
50+
"\$unionWith", Document("coll", "inspections")
51+
.append("pipeline", pipeline2)
52+
)
53+
unionWithStage.add(unionWith)
54+
val finalPipeline: MutableList<Document> = ArrayList(pipeline1)
55+
finalPipeline.addAll(unionWithStage)
56+
57+
val resultsFlow = collection.aggregate<Document>(finalPipeline)
58+
resultsFlow.collect { println(it) }
59+
60+
}
61+
mongoClient.close()
62+
}
63+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
@file:JvmName("SearchWithUnionwithQueryFacetKt")
2+
3+
import com.mongodb.kotlin.client.coroutine.MongoClient
4+
import kotlinx.coroutines.runBlocking
5+
import org.bson.Document
6+
import java.util.*
7+
8+
9+
fun main() {
10+
val uri = "<connection string>"
11+
val mongoClient = MongoClient.create(uri)
12+
val database = mongoClient.getDatabase("sample_training")
13+
val collection = database.getCollection<Document>("companies")
14+
15+
runBlocking {
16+
val searchStage = Document(
17+
"\$search", Document(
18+
"text",
19+
Document("query", "mobile")
20+
.append("path", "name")
21+
.append("score", Document("boost", Document("value", 1.6)))
22+
)
23+
)
24+
25+
val projectStage = Document(
26+
"\$project", Document("score", Document("\$meta", "searchScore"))
27+
.append("_id", 0)
28+
.append("number_of_employees", 1)
29+
.append("founded_year", 1)
30+
.append("name", 1)
31+
)
32+
33+
val addFieldsStage = Document(
34+
"\$addFields", Document("source", "companies")
35+
.append("source_count", "$\$SEARCH_META.count.lowerBound")
36+
)
37+
38+
val limitStage = Document("\$limit", 3)
39+
40+
val unionWithStage = Document(
41+
"\$unionWith", Document("coll", "inspections")
42+
.append(
43+
"pipeline", Arrays.asList(
44+
Document(
45+
"\$search", Document(
46+
"text",
47+
Document("query", "mobile")
48+
.append("path", "business_name")
49+
)
50+
),
51+
Document(
52+
"\$project", Document("score", Document("\$meta", "searchScore"))
53+
.append("business_name", 1)
54+
.append("address", 1)
55+
.append("_id", 0)
56+
),
57+
Document("\$limit", 3),
58+
Document(
59+
"\$set", Document("source", "inspections")
60+
.append("source_count", "$\$SEARCH_META.count.lowerBound")
61+
),
62+
Document("\$sort", Document("score", -1))
63+
)
64+
)
65+
)
66+
67+
val facetStage = Document(
68+
"\$facet", Document("allDocs", Arrays.asList<Any>())
69+
.append(
70+
"totalCount", Arrays.asList(
71+
Document(
72+
"\$group", Document("_id", "\$source")
73+
.append("firstCount", Document("\$first", "\$source_count"))
74+
),
75+
Document(
76+
"\$project", Document(
77+
"totalCount",
78+
Document("\$sum", "\$firstCount")
79+
)
80+
)
81+
)
82+
)
83+
)
84+
85+
val resultsFlow = collection.aggregate<Document>(
86+
listOf(
87+
searchStage,
88+
projectStage,
89+
addFieldsStage,
90+
limitStage,
91+
unionWithStage,
92+
facetStage
93+
)
94+
)
95+
resultsFlow.collect { println(it) }
96+
97+
}
98+
mongoClient.close()
99+
}
100+

source/examples/atlas-examples/SynonymsEquivalentQueryAdvanced.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fun main() {
1717
"\$search",
1818
Document("index", "synonyms-tutorial")
1919
.append(
20-
"autocomplete",
20+
"compound",
2121
Document(
2222
"should", listOf(
2323
Document(

0 commit comments

Comments
 (0)