Skip to content

Commit c925426

Browse files
authored
DOCSP-46391: client bulk write (#96)
1 parent dc2c976 commit c925426

29 files changed

+485
-123
lines changed

snooty.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ driver-long = "MongoDB Java Reactive Streams Driver"
2424
version = "5.3"
2525
full-version = "{+version+}.0"
2626
api = "https://mongodb.github.io/mongo-java-driver/{+version+}/apidocs"
27+
rs-driver-api = "{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client"
28+
core-api = "{+api+}/mongodb-driver-core/com/mongodb"
2729
rs-docs = "https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams"
2830
driver-source-gh = "https://github.com/mongodb/mongo-java-driver"
2931
java-rs = "Java Reactive Streams"

source/connect-to-mongo/choose-connection-target.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ API Documentation
142142
To learn more about creating a ``MongoClient`` instance in the {+driver-short+},
143143
see the following API documentation:
144144

145-
- `MongoClient <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClients.html>`__
145+
- `MongoClient <{+rs-driver-api+}/MongoClients.html>`__

source/connect-to-mongo/connection-options.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Authentication
6868

6969
* - **authMechanism**
7070
- | Sets the mechanism the {+driver-short+} uses to authenticate the
71-
| application. Valid options are defined in the `Class ConnectionString <{+api+}/mongodb-driver-core/com/mongodb/ConnectionString.html>`__ API documentation.
71+
| application. Valid options are defined in the `Class ConnectionString <{+core-api+}/ConnectionString.html>`__ API documentation.
7272
|
7373
| **Data Type**: {+string-data-type+}
7474
| **Default**: ``"SCRAM-SHA-256"`` when connecting to MongoDB v4.0 or later.
@@ -376,15 +376,15 @@ Read Preference Configuration
376376
| colon-separated key-value pairs, e.g. ``dc:ny,rack:1``.
377377
| Spaces are stripped from beginning and end of all keys and values. To
378378
| specify a list of tag sets, use multiple ``readPreferenceTags``, separated
379-
| by semicolons. To learn more about ``readPreferenceTags``, see the `Class ConnectionString <{+api+}/mongodb-driver-core/com/mongodb/ConnectionString.html>`__ API documentation.
379+
| by semicolons. To learn more about ``readPreferenceTags``, see the `Class ConnectionString <{+core-api+}/ConnectionString.html>`__ API documentation.
380380
|
381381
| **Data Type**: {+string-data-type+}
382382
| **Default**: ``null``
383383
| **Connection URI Example**: ``readPreferenceTags=dc:ny``
384384

385385
* - **maxStalenessSeconds**
386386
- | Sets the maximum staleness in seconds. To learn more about staleness,
387-
| see the `Class ConnectionString <{+api+}/mongodb-driver-core/com/mongodb/ConnectionString.html>`__ API documentation.
387+
| see the `Class ConnectionString <{+core-api+}/ConnectionString.html>`__ API documentation.
388388
|
389389
| **Data Type**: ``Integer``
390390
| **Default**: ``null``
@@ -471,7 +471,7 @@ SRV Configuration
471471

472472
* - **srvServiceName**
473473
- | Sets the SRV service name. See
474-
| the `ClusterSettings.getSrvServiceName() <{+api+}/mongodb-driver-core/com/mongodb/connection/ClusterSettings.html#getSrvServiceName()>`__
474+
| the `ClusterSettings.getSrvServiceName() <{+core-api+}/connection/ClusterSettings.html#getSrvServiceName()>`__
475475
| API documentation to learn more.
476476
|
477477
| **Data Type**: {+string-data-type+}

source/connect-to-mongo/create-a-mongo-client.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ API Documentation
117117
To learn more about creating a ``MongoClient`` instance in the {+driver-short+},
118118
see the following API documentation:
119119

120-
- `MongoClient <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClients.html>`__
120+
- `MongoClient <{+rs-driver-api+}/MongoClients.html>`__

source/connect-to-mongo/network-compression.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ API Documentation
9292
To learn more about any of the methods or types discussed in this
9393
guide, see the following API documentation:
9494

95-
- `MongoClient <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClient.html>`__
96-
- `createSnappyCompressor() <{+api+}/mongodb-driver-core/com/mongodb/MongoCompressor.html#createSnappyCompressor()>`__
95+
- `MongoClient <{+rs-driver-api+}/MongoClient.html>`__
96+
- `createSnappyCompressor() <{+core-api+}/MongoCompressor.html#createSnappyCompressor()>`__
9797
- `createZlibCompressor() <{+api+}//mongodb-driver-core/com/mongodb/MongoCompressor.html#createZlibCompressor()>`__
98-
- `createZstdCompressor() <{+api+}/mongodb-driver-core/com/mongodb/MongoCompressor.html#createZstdCompressor()>`__
98+
- `createZstdCompressor() <{+core-api+}/MongoCompressor.html#createZstdCompressor()>`__

source/connect-to-mongo/stable-api.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ API Documentation
122122
For more information about using the {+stable-api+} with the {+driver-short+}, see the
123123
following API documentation:
124124

125-
- `ServerApi <{+api+}/mongodb-driver-core/com/mongodb/ServerApi.html>`__
126-
- `ServerApi.Builder <{+api+}/mongodb-driver-core/com/mongodb/ServerApi.Builder.html>`__
127-
- `ServerApiVersion <{+api+}/mongodb-driver-core/com/mongodb/ServerApiVersion.html>`__
128-
- `ServerAddress <{+api+}/mongodb-driver-core/com/mongodb/ServerAddress.html>`__
129-
- `MongoClientSettings <{+api+}/mongodb-driver-core/com/mongodb/MongoClientSettings.html>`__
130-
- `MongoClientSettings.Builder <{+api+}/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html>`__
131-
- `MongoClients <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClients.html>`__
125+
- `ServerApi <{+core-api+}/ServerApi.html>`__
126+
- `ServerApi.Builder <{+core-api+}/ServerApi.Builder.html>`__
127+
- `ServerApiVersion <{+core-api+}/ServerApiVersion.html>`__
128+
- `ServerAddress <{+core-api+}/ServerAddress.html>`__
129+
- `MongoClientSettings <{+core-api+}/MongoClientSettings.html>`__
130+
- `MongoClientSettings.Builder <{+core-api+}/MongoClientSettings.Builder.html>`__
131+
- `MongoClients <{+rs-driver-api+}/MongoClients.html>`__

source/connect-to-mongo/tls.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Include the following import statements:
111111

112112
To instruct the driver to use
113113
`io.netty.handler.ssl.SslContext <https://netty.io/4.1/api/io/netty/handler/ssl/SslContext.html>`__,
114-
configure `NettyTransportSettings <{+api+}/mongodb-driver-core/com/mongodb/connection/NettyTransportSettings.html>`__
114+
configure `NettyTransportSettings <{+core-api+}/connection/NettyTransportSettings.html>`__
115115
when you define your ``MongoClientSettings``.
116116

117117
Use ``MongoClientSettings.Builder.transportSettings()``

source/data-formats/time-series.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ To create a time series collection, pass the following parameters to the
6969

7070
- The name of the new collection to create
7171

72-
- A `CreateCollectionOptions <{+api+}/mongodb-driver-core/com/mongodb/client/model/CreateCollectionOptions.html>`__
73-
object with the `TimeSeriesOptions <{+api+}/mongodb-driver-core/com/mongodb/client/model/TimeSeriesOptions.html>`__ set
72+
- A `CreateCollectionOptions <{+core-api+}/client/model/CreateCollectionOptions.html>`__
73+
object with the `TimeSeriesOptions <{+core-api+}/client/model/TimeSeriesOptions.html>`__ set
7474
with the ``timeSeriesOptions()`` method
7575

7676
.. _java-rs-time-series-create-example:
@@ -165,7 +165,7 @@ API Documentation
165165
To learn more about the methods mentioned in this guide, see the following
166166
API documentation:
167167

168-
- `createCollection() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#createCollection(java.lang.String)>`__
169-
- `listCollections() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#listCollections()>`__
170-
- `insertOne() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#insertOne(TDocument)>`__
171-
- `insertMany() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#insertMany(java.util.List)>`__
168+
- `createCollection() <{+rs-driver-api+}/MongoDatabase.html#createCollection(java.lang.String)>`__
169+
- `listCollections() <{+rs-driver-api+}/MongoDatabase.html#listCollections()>`__
170+
- `insertOne() <{+rs-driver-api+}/MongoCollection.html#insertOne(TDocument)>`__
171+
- `insertMany() <{+rs-driver-api+}/MongoCollection.html#insertMany(java.util.List)>`__
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.example;
2+
3+
import com.mongodb.*;
4+
import com.mongodb.client.model.*;
5+
import com.mongodb.client.model.bulk.*;
6+
import org.bson.Document;
7+
8+
import org.reactivestreams.Publisher;
9+
import reactor.core.publisher.Mono;
10+
11+
import com.mongodb.reactivestreams.client.MongoClient;
12+
import com.mongodb.reactivestreams.client.MongoClients;
13+
14+
import java.util.*;
15+
16+
public class QuickStart {
17+
public static void main(String[] args) {
18+
// Replace the placeholder with your Atlas connection string
19+
String uri = "<connection string>";
20+
21+
MongoClientSettings settings = MongoClientSettings.builder()
22+
.applyConnectionString(new ConnectionString(uri))
23+
.build();
24+
25+
// Create a new client and connect to the server
26+
try (MongoClient mongoClient = MongoClients.create(settings)) {
27+
28+
// start-insert-models
29+
ClientNamespacedInsertOneModel personToInsert = ClientNamespacedWriteModel
30+
.insertOne(
31+
new MongoNamespace("db", "people"),
32+
new Document("name", "Julia Smith")
33+
);
34+
35+
ClientNamespacedInsertOneModel thingToInsert = ClientNamespacedWriteModel
36+
.insertOne(
37+
new MongoNamespace("db", "things"),
38+
new Document("object", "washing machine")
39+
);
40+
// end-insert-models
41+
42+
// start-replace-models
43+
ClientNamespacedReplaceOneModel personReplacement = ClientNamespacedWriteModel
44+
.replaceOne(
45+
new MongoNamespace("db", "people"),
46+
Filters.eq("_id", 1),
47+
new Document("name", "Frederic Hilbert")
48+
);
49+
50+
ClientNamespacedReplaceOneModel thingReplacement = ClientNamespacedWriteModel
51+
.replaceOne(
52+
new MongoNamespace("db", "things"),
53+
Filters.eq("_id", 1),
54+
new Document("object", "potato")
55+
);
56+
// end-replace-models
57+
58+
// start-perform
59+
MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
60+
MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
61+
62+
List<ClientNamespacedWriteModel> bulkOperations = Arrays.asList(
63+
ClientNamespacedWriteModel
64+
.insertOne(
65+
peopleNamespace,
66+
new Document("name", "Corey Kopper")
67+
),
68+
ClientNamespacedWriteModel
69+
.replaceOne(
70+
thingsNamespace,
71+
Filters.eq("_id", 1),
72+
new Document("object", "potato")
73+
)
74+
);
75+
76+
Publisher<ClientBulkWriteResult> bulkWritePublisher = mongoClient
77+
.bulkWrite(bulkOperations);
78+
79+
ClientBulkWriteResult clientBulkResult = Mono
80+
.from(bulkWritePublisher)
81+
.block();
82+
83+
System.out.println(clientBulkResult.toString());
84+
// end-perform
85+
86+
// start-options
87+
MongoNamespace namespace = new MongoNamespace("db", "people");
88+
89+
ClientBulkWriteOptions options = ClientBulkWriteOptions
90+
.clientBulkWriteOptions()
91+
.ordered(false);
92+
93+
List<ClientNamespacedWriteModel> bulkOperations = Arrays.asList(
94+
ClientNamespacedWriteModel.insertOne(
95+
namespace,
96+
new Document("_id", 1).append("name", "Rudra Suraj")
97+
),
98+
// Causes a duplicate key error
99+
ClientNamespacedWriteModel.insertOne(
100+
namespace,
101+
new Document("_id", 1).append("name", "Mario Bianchi")
102+
),
103+
ClientNamespacedWriteModel.insertOne(
104+
namespace,
105+
new Document("name", "Wendy Zhang")
106+
)
107+
);
108+
109+
Publisher<ClientBulkWriteResult> bulkWritePublisher = mongoClient
110+
.bulkWrite(bulkOperations, options);
111+
// end-options
112+
}
113+
}
114+
}

source/monitoring.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The MongoDB Java driver organizes the events it defines into three categories:
6666
The following sections show how to monitor each event category.
6767

6868
For a full list of the events you can monitor,
69-
`see the event package of the MongoDB Java Driver <{+api+}/mongodb-driver-core/com/mongodb/event/package-summary.html>`__.
69+
`see the event package of the MongoDB Java Driver <{+core-api+}/event/package-summary.html>`__.
7070

7171
.. _java-rs-command-events:
7272

@@ -380,7 +380,7 @@ Oracle:
380380

381381
For more information about the ``JMXConnectionPoolListener`` class, see
382382
the API Documentation for
383-
`JMXConnectionPoolListener <{+api+}/mongodb-driver-core/com/mongodb/management/JMXConnectionPoolListener.html>`__.
383+
`JMXConnectionPoolListener <{+core-api+}/management/JMXConnectionPoolListener.html>`__.
384384

385385
Include the Driver in Your Distributed Tracing System
386386
-----------------------------------------------------
@@ -418,14 +418,14 @@ API Documentation
418418
For more information about the classes and methods mentioned in this document, see
419419
the following API Documentation:
420420

421-
- `MongoClients <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClients.html>`__
422-
- `MongoClientSettings <{+api+}/mongodb-driver-core/com/mongodb/MongoClientSettings.html>`__
423-
- `CommandListener <{+api+}/mongodb-driver-core/com/mongodb/event/CommandListener.html>`__
424-
- `CommandStartedEvent <{+api+}/mongodb-driver-core/com/mongodb/event/CommandStartedEvent.html>`__
425-
- `CommandSucceededEvent <{+api+}/mongodb-driver-core/com/mongodb/event/CommandSucceededEvent.html>`__
426-
- `CommandFailedEvent <{+api+}/mongodb-driver-core/com/mongodb/event/CommandFailedEvent.html>`__
427-
- `ClusterListener <{+api+}/mongodb-driver-core/com/mongodb/event/ClusterListener.html>`__
428-
- `ClusterDescriptionChangedEvent <{+api+}/mongodb-driver-core/com/mongodb/event/ClusterDescriptionChangedEvent.html>`__
429-
- `ConnectionPoolListener <{+api+}/mongodb-driver-core/com/mongodb/event/ConnectionPoolListener.html>`__
430-
- `ConnectionCheckedOutEvent <{+api+}/mongodb-driver-core/com/mongodb/event/ConnectionCheckedOutEvent.html>`__
431-
- `ConnectionCheckOutFailedEvent <{+api+}/mongodb-driver-core/com/mongodb/event/ConnectionCheckOutFailedEvent.html>`__
421+
- `MongoClients <{+rs-driver-api+}/MongoClients.html>`__
422+
- `MongoClientSettings <{+core-api+}/MongoClientSettings.html>`__
423+
- `CommandListener <{+core-api+}/event/CommandListener.html>`__
424+
- `CommandStartedEvent <{+core-api+}/event/CommandStartedEvent.html>`__
425+
- `CommandSucceededEvent <{+core-api+}/event/CommandSucceededEvent.html>`__
426+
- `CommandFailedEvent <{+core-api+}/event/CommandFailedEvent.html>`__
427+
- `ClusterListener <{+core-api+}/event/ClusterListener.html>`__
428+
- `ClusterDescriptionChangedEvent <{+core-api+}/event/ClusterDescriptionChangedEvent.html>`__
429+
- `ConnectionPoolListener <{+core-api+}/event/ConnectionPoolListener.html>`__
430+
- `ConnectionCheckedOutEvent <{+core-api+}/event/ConnectionCheckedOutEvent.html>`__
431+
- `ConnectionCheckOutFailedEvent <{+core-api+}/event/ConnectionCheckOutFailedEvent.html>`__

source/read-write-configuration.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ To learn more about any of the methods or types discussed in this
196196
guide, see the following API documentation:
197197

198198
- `WriteConcern <{+api+}//mongodb-driver-core/com/mongodb/WriteConcern.html>`__
199-
- `MongoDatabase.withWriteConcern <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#withWriteConcern(com.mongodb.WriteConcern)>`__
200-
- `MongoCollection.withWriteConcern <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#withWriteConcern(com.mongodb.WriteConcern)>`__
201-
- `ReadConcern <{+api+}/mongodb-driver-core/com/mongodb/ReadConcern.html>`__
202-
- `MongoDatabase.withReadConcern <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#withReadConcern(com.mongodb.ReadConcern)>`__
203-
- `MongoCollection.withReadConcern <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#withReadPreference(com.mongodb.ReadPreference)>`__
204-
- `ReadPreference <{+api+}/mongodb-driver-core/com/mongodb/ReadPreference.html>`__
205-
- `MongoDatabase.withReadPreference <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#withReadPreference(com.mongodb.ReadPreference)>`__
206-
- `MongoDatabase.withReadPreference <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#withReadPreference(com.mongodb.ReadPreference)>`__
199+
- `MongoDatabase.withWriteConcern <{+rs-driver-api+}/MongoDatabase.html#withWriteConcern(com.mongodb.WriteConcern)>`__
200+
- `MongoCollection.withWriteConcern <{+rs-driver-api+}/MongoCollection.html#withWriteConcern(com.mongodb.WriteConcern)>`__
201+
- `ReadConcern <{+core-api+}/ReadConcern.html>`__
202+
- `MongoDatabase.withReadConcern <{+rs-driver-api+}/MongoDatabase.html#withReadConcern(com.mongodb.ReadConcern)>`__
203+
- `MongoCollection.withReadConcern <{+rs-driver-api+}/MongoCollection.html#withReadPreference(com.mongodb.ReadPreference)>`__
204+
- `ReadPreference <{+core-api+}/ReadPreference.html>`__
205+
- `MongoDatabase.withReadPreference <{+rs-driver-api+}/MongoDatabase.html#withReadPreference(com.mongodb.ReadPreference)>`__
206+
- `MongoDatabase.withReadPreference <{+rs-driver-api+}/MongoCollection.html#withReadPreference(com.mongodb.ReadPreference)>`__

source/read/change-streams.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ API Documentation
237237
To learn more about any of the methods or types discussed in this
238238
guide, see the following API documentation:
239239

240-
- `MongoClient.watch() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClient.html#watch()>`__
241-
- `MongoCollection.watch() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#watch()>`__
242-
- `MongoDatabase.watch() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#watch()>`__
243-
- `ChangeStreamPublisher <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/ChangeStreamPublisher.html>`__
240+
- `MongoClient.watch() <{+rs-driver-api+}/MongoClient.html#watch()>`__
241+
- `MongoCollection.watch() <{+rs-driver-api+}/MongoCollection.html#watch()>`__
242+
- `MongoDatabase.watch() <{+rs-driver-api+}/MongoDatabase.html#watch()>`__
243+
- `ChangeStreamPublisher <{+rs-driver-api+}/ChangeStreamPublisher.html>`__

source/read/count-documents.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,6 @@ API Documentation
227227
To learn more about any of the methods or types discussed in this
228228
guide, see the following API documentation:
229229

230-
- `countDocuments() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#countDocuments()>`__
231-
- `estimatedDocumentCount() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#estimatedDocumentCount()>`__
232-
- `Collation <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/FindPublisher.html#collation(com.mongodb.client.model.Collation)>`__
230+
- `countDocuments() <{+rs-driver-api+}/MongoCollection.html#countDocuments()>`__
231+
- `estimatedDocumentCount() <{+rs-driver-api+}/MongoCollection.html#estimatedDocumentCount()>`__
232+
- `Collation <{+rs-driver-api+}/FindPublisher.html#collation(com.mongodb.client.model.Collation)>`__

0 commit comments

Comments
 (0)