Skip to content

Commit 2b5b815

Browse files
committed
merge upstream
2 parents 050ac55 + eb4b830 commit 2b5b815

26 files changed

+782
-134
lines changed

examples/src/test/kotlin/AggregatesBuilderTest.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import config.getConfig
3434
import kotlinx.coroutines.flow.firstOrNull
3535
import kotlinx.coroutines.flow.toList
3636
import kotlinx.coroutines.runBlocking
37+
import org.bson.BinaryVector
3738
import org.bson.Document
3839
import org.bson.codecs.pojo.annotations.BsonId
3940
import org.bson.types.ObjectId
@@ -970,7 +971,8 @@ class AggregatesBuilderTest {
970971
assertEquals(1, results.first().get("count", Document::class.java).get("lowerBound", java.lang.Long::class.java)?.toInt())
971972
}
972973

973-
/* NOTE: Test is not run by default. Vector search requires the creation of a vector search index on the collection before running.
974+
/* NOTE: Test is not run by default. Vector search requires the creation of
975+
a vector search index on the collection before running.
974976
*/
975977
@Ignore
976978
fun vectorSearchTest() = runBlocking {
@@ -979,7 +981,7 @@ class AggregatesBuilderTest {
979981
// :snippet-start: vector-search
980982
Aggregates.vectorSearch(
981983
SearchPath.fieldPath(MovieAlt::plotEmbedding.name),
982-
listOf(-0.0072121937, -0.030757688, -0.012945653),
984+
BinaryVector.floatVector(floatArrayOf(0.0001f, 1.12345f, 2.23456f, 3.34567f, 4.45678f)),
983985
"mflix_movies_embedding_index",
984986
1.toLong(),
985987
exactVectorSearchOptions().filter(Filters.gte(MovieAlt::year.name, 2016))

examples/src/test/kotlin/BulkTest.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import com.mongodb.client.model.DeleteOneModel
66
import com.mongodb.client.model.Filters
77
import com.mongodb.client.model.InsertOneModel
88
import com.mongodb.client.model.ReplaceOneModel
9+
import com.mongodb.client.model.ReplaceOptions
10+
import com.mongodb.client.model.Sorts
911
import com.mongodb.client.model.UpdateOneModel
12+
import com.mongodb.client.model.UpdateOptions
1013
import com.mongodb.client.model.Updates
1114
import com.mongodb.kotlin.client.coroutine.MongoClient
1215
import config.getConfig
@@ -21,17 +24,18 @@ import org.junit.jupiter.api.BeforeAll
2124
import org.junit.jupiter.api.Test
2225
import org.junit.jupiter.api.TestInstance
2326

24-
// :snippet-start: bulk-data-model
25-
data class Person(
26-
@BsonId val id: Int,
27-
val name: String,
28-
val age: Int? = null,
29-
val location: String? = null
30-
)
31-
// :snippet-end:
32-
3327
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
3428
internal class BulkTest {
29+
30+
// :snippet-start: bulk-data-model
31+
data class Person(
32+
@BsonId val id: Int,
33+
val name: String,
34+
val age: Int? = null,
35+
val location: String? = null
36+
)
37+
// :snippet-end:
38+
3539
companion object {
3640
val config = getConfig()
3741
val client = MongoClient.create(config.connectionUri)
@@ -95,6 +99,11 @@ internal class BulkTest {
9599
val insert = Person(1, "Celine Stork", location = "San Diego, CA")
96100
val doc = ReplaceOneModel(filter, insert)
97101
// :snippet-end:
102+
103+
// :snippet-start: replace-model-options
104+
val opts = ReplaceOptions().sort(Sorts.ascending("_id"))
105+
// :snippet-end:
106+
98107
// Junit test for the above code
99108
val insertTest = collection.bulkWrite(listOf(doc))
100109
assertTrue(insertTest.wasAcknowledged())
@@ -107,6 +116,11 @@ internal class BulkTest {
107116
val update = Updates.inc(Person::age.name, 1)
108117
val doc = UpdateOneModel<Person>(filter, update)
109118
// :snippet-end:
119+
120+
// :snippet-start: update-model-options
121+
val opts = UpdateOptions().sort(Sorts.ascending("_id"))
122+
// :snippet-end:
123+
110124
// Junit test for the above code
111125
val updateTest = collection.bulkWrite(listOf(doc))
112126
assertTrue(updateTest.wasAcknowledged())

examples/src/test/kotlin/ChangeTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22
import com.mongodb.client.model.Filters
3+
import com.mongodb.client.model.ReplaceOptions
4+
import com.mongodb.client.model.Sorts
5+
import com.mongodb.client.model.UpdateOptions
36
import com.mongodb.client.model.Updates
47
import com.mongodb.kotlin.client.coroutine.MongoClient
58
import config.getConfig
@@ -63,6 +66,11 @@ internal class ChangeTest {
6366
println("Matched document count: $result.matchedCount")
6467
println("Modified document count: $result.modifiedCount")
6568
// :snippet-end:
69+
70+
// :snippet-start: update-one-options
71+
val opts = UpdateOptions().sort(Sorts.ascending(PaintOrder::color.name))
72+
// :snippet-end:
73+
6674
// Junit test for the above code
6775
assertEquals(1, result.modifiedCount)
6876
}
@@ -90,6 +98,11 @@ internal class ChangeTest {
9098
println("Matched document count: $result.matchedCount")
9199
println("Modified document count: $result.modifiedCount")
92100
// :snippet-end:
101+
102+
// :snippet-start: replace-one-options
103+
val opts = ReplaceOptions().sort(Sorts.ascending(PaintOrder::color.name))
104+
// :snippet-end:
105+
93106
// Junit test for the above code
94107
assertEquals(1, result.modifiedCount)
95108
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import com.mongodb.MongoNamespace
2+
import com.mongodb.client.model.Filters
3+
import com.mongodb.client.model.bulk.ClientBulkWriteOptions
4+
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
5+
import com.mongodb.kotlin.client.coroutine.MongoClient
6+
import config.getConfig
7+
import kotlinx.coroutines.runBlocking
8+
import org.bson.codecs.pojo.annotations.BsonId
9+
import org.junit.jupiter.api.AfterAll
10+
import org.junit.jupiter.api.Assertions.assertEquals
11+
import org.junit.jupiter.api.BeforeAll
12+
import org.junit.jupiter.api.TestInstance
13+
import kotlin.test.Ignore
14+
15+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
16+
internal class ClientBulkTest {
17+
18+
// :snippet-start: data-classes
19+
data class Person(
20+
@BsonId val id: Int,
21+
val name: String,
22+
)
23+
24+
data class Object(
25+
@BsonId val id: Int,
26+
val type: String,
27+
)
28+
// :snippet-end:
29+
30+
31+
companion object {
32+
val config = getConfig()
33+
val client = MongoClient.create(config.connectionUri)
34+
val database = client.getDatabase("sample_db")
35+
val personCollection = database.getCollection<Person>("people")
36+
val objectCollection = database.getCollection<Object>("objects")
37+
38+
@BeforeAll
39+
@JvmStatic
40+
fun beforeAll() {
41+
runBlocking {
42+
personCollection.insertOne(Person(1, "Sandy King"))
43+
objectCollection.insertOne(Object(1, "artist easel"))
44+
}
45+
}
46+
47+
@AfterAll
48+
@JvmStatic
49+
fun afterAll() {
50+
runBlocking {
51+
personCollection.drop()
52+
objectCollection.drop()
53+
client.close()
54+
}
55+
}
56+
}
57+
58+
// Ignoring tests because successful completion of
59+
// writes is blocked on https://jira.mongodb.org/browse/CLOUDP-288992
60+
@Ignore
61+
fun insertOperationTest() = runBlocking {
62+
// :snippet-start: insert-models
63+
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
64+
65+
docsToInsert.add(ClientNamespacedWriteModel
66+
.insertOne(
67+
MongoNamespace("sample_db", "people"),
68+
Person(2, "Julia Smith")
69+
)
70+
)
71+
72+
docsToInsert.add(ClientNamespacedWriteModel
73+
.insertOne(
74+
MongoNamespace("sample_db", "objects"),
75+
Object(2, "washing machine")
76+
)
77+
)
78+
79+
val clientBulkResult = client.bulkWrite(docsToInsert)
80+
// :snippet-end:
81+
82+
// Junit test for the above code
83+
assertEquals(2, objectCollection.countDocuments())
84+
assertEquals(2, personCollection.countDocuments())
85+
}
86+
87+
@Ignore
88+
fun replaceOperationTest() = runBlocking {
89+
// :snippet-start: replace-models
90+
val docsReplacements = mutableListOf<ClientNamespacedWriteModel>()
91+
92+
docsReplacements.add(ClientNamespacedWriteModel
93+
.replaceOne(
94+
MongoNamespace("sample_db", "people"),
95+
Filters.eq(Person::id.name, 1),
96+
Person(1, "Frederic Hilbert")
97+
)
98+
)
99+
100+
docsReplacements.add(ClientNamespacedWriteModel
101+
.replaceOne(
102+
MongoNamespace("sample_db", "objects"),
103+
Filters.eq(Object::id.name, 1),
104+
Object(1, "ironing board")
105+
)
106+
)
107+
108+
val clientBulkResult = client.bulkWrite(docsReplacements)
109+
// :snippet-end:
110+
111+
// Junit test for the above code
112+
assertEquals(1, objectCollection.countDocuments())
113+
}
114+
115+
@Ignore
116+
fun orderOfOperationsTest() = runBlocking {
117+
// :snippet-start: options
118+
val namespace = MongoNamespace("sample_db", "people")
119+
120+
val options = ClientBulkWriteOptions
121+
.clientBulkWriteOptions()
122+
.ordered(false)
123+
124+
val bulkOperations = listOf(
125+
ClientNamespacedWriteModel.insertOne(
126+
namespace,
127+
Person(2, "Rudra Suraj")
128+
),
129+
// Causes duplicate key error
130+
ClientNamespacedWriteModel.insertOne(
131+
namespace,
132+
Person(2, "Wendy Zhang")
133+
),
134+
ClientNamespacedWriteModel.insertOne(
135+
namespace,
136+
Person(4, "Mario Bianchi")
137+
)
138+
)
139+
140+
val result = client.bulkWrite(bulkOperations, options)
141+
// :snippet-end:
142+
143+
// Junit test for the above code
144+
assertEquals(3, personCollection.countDocuments())
145+
}
146+
}

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ toc_landing_pages = [
1212
"/fundamentals/builders",
1313
"/usage-examples",
1414
"/api-documentation",
15+
"/fundamentals/builders/aggregates",
1516
]
1617
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
1718

source/examples/generated/AggregatesBuilderTest.snippet.vector-search.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Aggregates.vectorSearch(
22
SearchPath.fieldPath(MovieAlt::plotEmbedding.name),
3-
listOf(-0.0072121937, -0.030757688, -0.012945653),
3+
BinaryVector.floatVector(floatArrayOf(0.0001f, 1.12345f, 2.23456f, 3.34567f, 4.45678f)),
44
"mflix_movies_embedding_index",
55
1.toLong(),
66
exactVectorSearchOptions().filter(Filters.gte(MovieAlt::year.name, 2016))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val opts = ReplaceOptions().sort(Sorts.ascending("_id"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val opts = UpdateOptions().sort(Sorts.ascending("_id"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val opts = ReplaceOptions().sort(Sorts.ascending(PaintOrder::color.name))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val opts = UpdateOptions().sort(Sorts.ascending(PaintOrder::color.name))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
data class Person(
2+
@BsonId val id: Int,
3+
val name: String,
4+
)
5+
6+
data class Object(
7+
@BsonId val id: Int,
8+
val type: String,
9+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
2+
3+
docsToInsert.add(ClientNamespacedWriteModel
4+
.insertOne(
5+
MongoNamespace("sample_db", "people"),
6+
Person(2, "Julia Smith")
7+
)
8+
)
9+
10+
docsToInsert.add(ClientNamespacedWriteModel
11+
.insertOne(
12+
MongoNamespace("sample_db", "objects"),
13+
Object(2, "washing machine")
14+
)
15+
)
16+
17+
val clientBulkResult = client.bulkWrite(docsToInsert)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
val namespace = MongoNamespace("sample_db", "people")
2+
3+
val options = ClientBulkWriteOptions
4+
.clientBulkWriteOptions()
5+
.ordered(false)
6+
7+
val bulkOperations = listOf(
8+
ClientNamespacedWriteModel.insertOne(
9+
namespace,
10+
Person(2, "Rudra Suraj")
11+
),
12+
// Causes duplicate key error
13+
ClientNamespacedWriteModel.insertOne(
14+
namespace,
15+
Person(2, "Wendy Zhang")
16+
),
17+
ClientNamespacedWriteModel.insertOne(
18+
namespace,
19+
Person(4, "Mario Bianchi")
20+
)
21+
)
22+
23+
val result = client.bulkWrite(bulkOperations, options)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
val docsReplacements = mutableListOf<ClientNamespacedWriteModel>()
2+
3+
docsReplacements.add(ClientNamespacedWriteModel
4+
.replaceOne(
5+
MongoNamespace("sample_db", "people"),
6+
Filters.eq(Person::id.name, 1),
7+
Person(1, "Frederic Hilbert")
8+
)
9+
)
10+
11+
docsReplacements.add(ClientNamespacedWriteModel
12+
.replaceOne(
13+
MongoNamespace("sample_db", "objects"),
14+
Filters.eq(Object::id.name, 1),
15+
Object(1, "ironing board")
16+
)
17+
)
18+
19+
val clientBulkResult = client.bulkWrite(docsReplacements)

source/fundamentals.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Fundamentals
1414
Stable API </fundamentals/stable-api>
1515
Databases & Collections </fundamentals/databases-collections>
1616
Data Formats </fundamentals/data-formats>
17-
CRUD Operations /fundamentals/crud
17+
CRUD Operations </fundamentals/crud>
1818
Builders </fundamentals/builders>
1919
Aggregation </fundamentals/aggregation>
2020
Aggregation Expressions </fundamentals/aggregation-expression-operations>

source/fundamentals/builders.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,21 @@ builders in the {+driver-short+}:
108108
Available Builders
109109
------------------
110110

111-
- :ref:`Aggregates <aggregates-builders>` for building aggregation pipelines.
111+
- :ref:`Aggregates <aggregates-builders>` for building aggregation
112+
pipelines.
113+
114+
- :ref:`kotlin-atlas-vector-search` for using the
115+
``Aggregates.vectorSearch()`` method to use the Atlas Vector
116+
Search feature.
117+
112118
- :ref:`Filters <filters-builders>` for building query filters.
119+
113120
- :ref:`Indexes <indexes-builders>` for creating index keys.
114-
- :ref:`Projections <projections-builders>` for building projections.
121+
122+
- :ref:`Projections <projections-builders>` for building projections.
123+
115124
- :ref:`Sorts <sorts-builders>` for building sort criteria.
125+
116126
- :ref:`Updates <updates-builders>` for building updates.
117127

118128
The :ref:`kotlin-builders-data-classes` guide provides examples on

0 commit comments

Comments
 (0)