-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-41769: Improved bulk write API #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
72b9e73
09d5786
161ffec
9d37fac
5e577ba
78d26c0
db24b1e
56413a0
7308593
c30dacd
08baf89
fa4ff13
578cb8d
1c94b5c
ad5ea28
d9a33d9
02b1720
ef63a45
1cfd451
7110a7f
68e6074
9934475
f43f5c9
605cf7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,16 @@ Overview | |||||||||
In this guide, you can learn how to use bulk operations in the | ||||||||||
MongoDB Java Driver. | ||||||||||
|
||||||||||
.. note:: Client Bulk Write Method | ||||||||||
|
||||||||||
This guide primarily focuses on the ``MongoCollection.bulkWrite()`` | ||||||||||
method, which allows you to perform bulk operations on a collection. | ||||||||||
When connecting to a deployment running {+mdb-server+} 8.0 or later, | ||||||||||
you can use the ``MongoClient.bulkWrite()`` method to write | ||||||||||
to multiple databases and collections in the same call. See the | ||||||||||
:ref:`java-sync-client-bulk-write` section below to learn how to use the | ||||||||||
``MongoClient.bulkWrite()`` method. | ||||||||||
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
To perform a create, replace, update, or delete operation, | ||||||||||
use its corresponding method. For example, to insert one document, | ||||||||||
update multiple documents, and delete one document in your collection, | ||||||||||
|
@@ -44,7 +54,7 @@ document. The examples in each section use the following documents in the | |||||||||
{ "_id": 8, "name": "Shayla Ray", "age": 20 } | ||||||||||
|
||||||||||
For more information about the methods and classes mentioned in this section, | ||||||||||
see the following API Documentation: | ||||||||||
see the following API documentation: | ||||||||||
mayaraman19 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
- `bulkWrite() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#bulkWrite(java.util.List,com.mongodb.client.model.BulkWriteOptions)>`__ | ||||||||||
- `WriteModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/WriteModel.html>`__ | ||||||||||
|
@@ -100,7 +110,7 @@ describing people: | |||||||||
For more information about the methods and classes mentioned in this section, | ||||||||||
see the `InsertOneModel | ||||||||||
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/InsertOneModel.html>`__ | ||||||||||
API Documentation. | ||||||||||
API documentation. | ||||||||||
|
||||||||||
Replace Operation | ||||||||||
~~~~~~~~~~~~~~~~~ | ||||||||||
|
@@ -132,7 +142,7 @@ contains an added ``location`` field: | |||||||||
For more information about the methods and classes mentioned in this section, | ||||||||||
see the following resources: | ||||||||||
|
||||||||||
- `ReplaceOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOneModel.html>`__ API Documentation | ||||||||||
- `ReplaceOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOneModel.html>`__ API documentation | ||||||||||
- :manual:`Unique indexes </core/index-unique/>` Server Manual Explanation | ||||||||||
|
||||||||||
Update Operation | ||||||||||
|
@@ -168,8 +178,8 @@ the ``age`` field in a document where the ``_id`` is ``2``: | |||||||||
For more information about the methods and classes mentioned in this section, | ||||||||||
see the following resources: | ||||||||||
|
||||||||||
- `UpdateOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOneModel.html>`__ API Documentation | ||||||||||
- `UpdateManyModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateManyModel.html>`__ API Documentation | ||||||||||
- `UpdateOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOneModel.html>`__ API documentation | ||||||||||
- `UpdateManyModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateManyModel.html>`__ API documentation | ||||||||||
- :manual:`unique indexes </core/index-unique/>` Server Manual Explanation | ||||||||||
|
||||||||||
Delete Operation | ||||||||||
|
@@ -202,7 +212,7 @@ a document where the ``_id`` is ``1``: | |||||||||
:end-before: end deleteDocumentsExample | ||||||||||
|
||||||||||
For more information about the methods and classes mentioned in this section, | ||||||||||
see the following API Documentation: | ||||||||||
see the following API documentation: | ||||||||||
|
||||||||||
- `DeleteOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteOneModel.html>`__ | ||||||||||
- `DeleteManyModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteManyModel.html>`__ | ||||||||||
|
@@ -288,14 +298,146 @@ operations to execute in any order: | |||||||||
{ "_id": 6, "name": "Zaynab Omar", "age": 37 } | ||||||||||
|
||||||||||
For more information about the methods and classes mentioned in this section, | ||||||||||
see the following API Documentation: | ||||||||||
see the following API documentation: | ||||||||||
|
||||||||||
- `BulkWriteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html>`__ | ||||||||||
- `ordered() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html#ordered(boolean)>`__ | ||||||||||
|
||||||||||
.. _java-sync-client-bulk-write: | ||||||||||
|
||||||||||
Client Bulk Write Method | ||||||||||
------------------------ | ||||||||||
|
||||||||||
When connecting to a deployment running {+mdb-server+} 8.0 or later, | ||||||||||
you can use the ``MongoClient.bulkWrite()`` method to write | ||||||||||
to multiple databases and collections in the same cluster. | ||||||||||
|
||||||||||
In the examples in preceding sections of this page, the ``MongoCollection.bulkWrite()`` method | ||||||||||
takes a list of ``WriteModel`` instances in which the specified subclass of | ||||||||||
``WriteModel`` represents the corresponding write operation. For example, an | ||||||||||
instance of ``InsertOneModel`` represents an operation to insert one document. | ||||||||||
|
||||||||||
Similarly, the ``MongoClient.bulkWrite()`` method takes a | ||||||||||
|
||||||||||
list of ``ClientNamespacedWriteModel`` instances to represent different write operations. | ||||||||||
However, you do not need to specify the subclass that represents the corresponding | ||||||||||
write operation. Instead, the ``ClientNamespacedWriteModel`` object contains different | ||||||||||
methods to represent different write operations, such as ``ClientNamespacedWriteModel.insertOne()`` or | ||||||||||
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
``ClientNamespacedWriteModel.updateOne()``. | ||||||||||
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
These methods take a ``MongoNamespace`` object that defines which | ||||||||||
database and collection to write to, and a ``Document`` object that defines | ||||||||||
stIncMale marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
information about the write operation. Some methods, such as ``updateOne()`` and | ||||||||||
stIncMale marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
``replaceOne()``, also take a ``Filters`` object that defines the filter for the operation. | ||||||||||
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
The following sections describe how to use the client ``bulkWrite()`` method. | ||||||||||
|
||||||||||
Insert Example | ||||||||||
~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
This following example shows how to use the ``bulkWrite()`` method to insert | ||||||||||
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
two documents. One document is inserted into the ``people`` collection in a | ||||||||||
database named ``db``. The other document is inserted into a collection called ``things`` | ||||||||||
in the ``db`` database. | ||||||||||
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
We use the ``MongoNamespace`` object to define the database and collection we | ||||||||||
want each write operation to be applied to. | ||||||||||
|
We use the ``MongoNamespace`` object to define the database and collection we | |
want each write operation to be applied to. | |
The example defines ``MongoNamespace`` instances to define the databases and collections | |
to which each write operation applies. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
existing documents in the ``people`` and ``things`` collections. | |
existing documents in the ``people`` and ``things`` collections of the ``db`` database. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to "db.people" and "db.things" for consistency with insert section
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
mayaraman19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the ``ClientBulkWriteOptions`` interface to specify options when | |
running the ``bulkWrite()`` method. | |
You can pass an instance of the ``ClientBulkWriteOptions`` interface to specify options when | |
running the ``bulkWrite()`` method. |
Uh oh!
There was an error while loading. Please reload this page.