Skip to content

Commit d7f5285

Browse files
committed
DOCSP-46389: client bw (#60)
* DOCSP-46389: client bw * wip (cherry picked from commit 88dc621)
1 parent 515941e commit d7f5285

File tree

4 files changed

+401
-43
lines changed

4 files changed

+401
-43
lines changed

source/includes/write/bulk.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ import com.mongodb.client.model.*
22
import com.mongodb.client.model.Filters.*
33
import com.mongodb.kotlin.client.MongoClient
44

5-
// start-data-class
5+
// start-data-classes
66
data class Restaurant(
77
val name: String,
88
val borough: String,
99
val cuisine: String
1010
)
11-
// end-data-class
11+
12+
data class Movie(
13+
val title: String,
14+
val year: Int
15+
)
16+
// end-data-classes
1217

1318
fun main() {
1419
val uri = "<connection string>"
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import com.mongodb.MongoNamespace
2+
import com.mongodb.client.model.Filters.eq
3+
import com.mongodb.client.model.bulk.ClientBulkWriteOptions
4+
import com.mongodb.client.model.bulk.ClientBulkWriteResult
5+
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
6+
import com.mongodb.kotlin.client.MongoClient
7+
import org.bson.Document
8+
9+
10+
// start-data-classes
11+
data class Restaurant(
12+
val name: String,
13+
val borough: String,
14+
val cuisine: String,
15+
)
16+
17+
data class Movie(
18+
val title: String,
19+
val year: Int
20+
)
21+
// end-data-classes
22+
23+
fun main() {
24+
val uri = "<connection string>"
25+
26+
val mongoClient = MongoClient.create(uri)
27+
28+
// start-insert-models
29+
val restaurantToInsert = ClientNamespacedWriteModel
30+
.insertOne(
31+
MongoNamespace("sample_restaurants", "restaurants"),
32+
Restaurant("Blue Moon Grill", "Brooklyn", "American")
33+
)
34+
35+
val movieToInsert = ClientNamespacedWriteModel
36+
.insertOne(
37+
MongoNamespace("sample_mflix", "movies"),
38+
Movie("Silly Days", 2022)
39+
)
40+
// end-insert-models
41+
42+
// start-replace-models
43+
val restaurantReplacement = ClientNamespacedWriteModel
44+
.replaceOne(
45+
MongoNamespace("sample_restaurants", "restaurants"),
46+
Filters.eq("_id", 1),
47+
Restaurant("Smith Town Diner", "Brooklyn", "American")
48+
)
49+
50+
val movieReplacement = ClientNamespacedWriteModel
51+
.replaceOne(
52+
MongoNamespace("sample_mflix", "movies"),
53+
Filters.eq("_id", 1),
54+
Movie("Loving Sylvie", 1999)
55+
)
56+
// end-replace-models
57+
58+
// start-perform
59+
val restaurantNamespace = MongoNamespace("sample_restaurants", "restaurants")
60+
val movieNamespace = MongoNamespace("sample_mflix", "movies")
61+
62+
val bulkOperations = listOf(
63+
ClientNamespacedWriteModel
64+
.insertOne(
65+
restaurantNamespace,
66+
Restaurant("Drea's", "Brooklyn", "Mexican")
67+
),
68+
ClientNamespacedWriteModel
69+
.replaceOne(
70+
movieNamespace,
71+
Filters.eq("_id", 1),
72+
Movie("Underneath It All", 2002)
73+
)
74+
)
75+
76+
val clientBulkResult = mongoClient
77+
.bulkWrite(bulkOperations)
78+
79+
println(clientBulkResult.toString())
80+
// end-perform
81+
82+
// start-options
83+
val namespace = MongoNamespace("sample_restaurants", "restaurants")
84+
85+
val options = ClientBulkWriteOptions
86+
.clientBulkWriteOptions()
87+
.ordered(false)
88+
89+
val bulkOps = listOf(
90+
ClientNamespacedWriteModel.insertOne(
91+
namespace,
92+
Document("_id", 1).append("name", "Freezyland")
93+
),
94+
// Causes a duplicate key error
95+
ClientNamespacedWriteModel.insertOne(
96+
namespace,
97+
Document("_id", 1).append("name", "Coffee Stand No. 1")
98+
),
99+
ClientNamespacedWriteModel.insertOne<Any>(
100+
namespace,
101+
Document("name", "Kelly's Krepes")
102+
)
103+
)
104+
105+
val result= mongoClient
106+
.bulkWrite(bulkOps, options)
107+
// end-options
108+
}
109+

source/whats-new.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ improvements, and fixes:
4242
:ref:`kotlin-sync-write-replace`, and
4343
:ref:`kotlin-sync-bulk-write` guides
4444

45+
- Implements a *client* bulk write API that allows you to perform write
46+
operations on multiple databases and collections in the same call. To learn
47+
more about this feature, see the :ref:`kotlin-sync-client-bulk-write`
48+
section of the Bulk Write Operations guide.
49+
4550
.. _kotlin-sync-version-5.2:
4651

4752
What's New in 5.2

0 commit comments

Comments
 (0)