Skip to content

Commit 4036891

Browse files
authored
DOCSP-46427: client bulk write (#111)
* DOCSP-46427: client BW * small fixes * MW PR fixes 1 * VB tech review 1
1 parent 88383e1 commit 4036891

File tree

5 files changed

+464
-55
lines changed

5 files changed

+464
-55
lines changed

source/includes/write/bulk-write.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object BulkWrite {
3232
// start-bulk-update-many
3333
val updateManyFilter = equal("name", "Wendy's")
3434
val updateManyDoc = set("cuisine", "Fast food")
35-
val updateManyModel = UpdateOneModel(updateManyFilter, updateManyDoc)
35+
val updateManyModel = UpdateManyModel(updateManyFilter, updateManyDoc)
3636
// end-bulk-update-many
3737

3838
// start-bulk-replace-one
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package quickstart
2+
3+
import org.mongodb.scala._
4+
import org.mongodb.scala.model.Filters._
5+
import com.mongodb.client.model.bulk.{ClientBulkWriteOptions, ClientBulkWriteResult, ClientNamespacedWriteModel}
6+
import org.mongodb.scala.model.Updates._
7+
8+
object Main {
9+
10+
def main(args: Array[String]): Unit = {
11+
val mongoClient = MongoClient("<connection string>")
12+
13+
{
14+
// start-insert-models
15+
val personToInsert = ClientNamespacedWriteModel.insertOne(
16+
MongoNamespace("db", "people"),
17+
Document("name" -> "Julia Smith")
18+
)
19+
20+
val thingToInsert = ClientNamespacedWriteModel.insertOne(
21+
MongoNamespace("db", "things"),
22+
Document("object" -> "washing machine")
23+
);
24+
// end-insert-models
25+
}
26+
{
27+
// start-update-models
28+
val personUpdate = ClientNamespacedWriteModel.updateOne(
29+
MongoNamespace("db", "people"),
30+
equal("name", "Freya Polk"),
31+
inc("age", 1)
32+
)
33+
34+
val thingUpdate = ClientNamespacedWriteModel.updateMany(
35+
MongoNamespace("db", "things"),
36+
equal("category", "electronic"),
37+
set("manufacturer", "Premium Technologies")
38+
)
39+
// end-update-models
40+
}
41+
42+
{
43+
// start-replace-models
44+
val personReplacement = ClientNamespacedWriteModel.replaceOne(
45+
MongoNamespace("db", "people"),
46+
equal("_id", 1),
47+
Document("name" -> "Frederic Hilbert")
48+
)
49+
50+
val thingReplacement = ClientNamespacedWriteModel.replaceOne(
51+
MongoNamespace("db", "things"),
52+
equal("_id", 1),
53+
Document("object" -> "potato")
54+
)
55+
// end-replace-models
56+
}
57+
58+
{
59+
// start-perform
60+
val peopleNamespace = MongoNamespace("db", "people")
61+
val thingsNamespace = MongoNamespace("db", "things")
62+
63+
val writeModels = List(
64+
ClientNamespacedWriteModel.insertOne(
65+
peopleNamespace,
66+
Document("name" -> "Corey Kopper")
67+
),
68+
ClientNamespacedWriteModel.replaceOne(
69+
thingsNamespace,
70+
equal("_id", 1),
71+
Document("object" -> "potato")
72+
)
73+
)
74+
75+
val observable = mongoClient.bulkWrite(writeModels)
76+
77+
observable.subscribe(
78+
(result: ClientBulkWriteResult) => println(result.toString),
79+
(error: Throwable) => println(s"Error: ${error.getMessage}"),
80+
() => println("Completed")
81+
)
82+
// end-perform
83+
}
84+
{
85+
// start-options
86+
val namespace = MongoNamespace("db", "people")
87+
88+
val options = ClientBulkWriteOptions.clientBulkWriteOptions().ordered(false)
89+
90+
val writeModels = List(
91+
ClientNamespacedWriteModel.insertOne(namespace, Document("_id" -> 1, "name" -> "Rudra Suraj")),
92+
// Causes a duplicate key error
93+
ClientNamespacedWriteModel.insertOne(namespace, Document("_id" -> 1, "name" -> "Mario Bianchi")),
94+
ClientNamespacedWriteModel.insertOne(namespace, Document("name" -> "Wendy Zhang"))
95+
)
96+
97+
val observable = mongoClient.bulkWrite(writeModels, options)
98+
// end-options
99+
}
100+
101+
Thread.sleep(1000)
102+
mongoClient.close()
103+
104+
}
105+
106+
}

source/whats-new.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,26 @@ What's New
2020
Learn about new features, improvements, and fixes introduced in the
2121
following versions of the {+driver-long+}:
2222

23+
* :ref:`Version 5.4 <scala-version-5.4>`
2324
* :ref:`Version 5.3 <scala-version-5.3>`
2425
* :ref:`Version 5.2 <scala-version-5.2>`
2526
* :ref:`Version 5.1.3 <scala-version-5.1.3>`
2627
* :ref:`Version 5.1.1 <scala-version-5.1.1>`
2728
* :ref:`Version 5.1 <scala-version-5.1>`
2829

30+
.. _scala-version-5.4:
31+
32+
What's New in 5.4
33+
-----------------
34+
35+
The 5.4 driver release includes the following changes, fixes,
36+
and features:
37+
38+
- Implements a *client* bulk write API that allows you to perform write
39+
operations on multiple databases and collections in the same call. To learn
40+
more about this feature, see the :ref:`scala-client-bulk-write`
41+
section of the Bulk Write Operations guide.
42+
2943
.. _scala-version-5.3:
3044

3145
What's New in 5.3

0 commit comments

Comments
 (0)