-
Notifications
You must be signed in to change notification settings - Fork 21
DOCSP-39895: Bulk write guide #117
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
DOCSP-39895: Bulk write guide #117
Conversation
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.
Good work so far! Lmk if you have any questions about my feedback.
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use **bulk operations**. |
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.
In this guide, you can learn how to use **bulk operations**. | |
In this guide, you can learn how to perform **bulk operations**. |
In this guide, you can learn how to use **bulk operations**. | ||
|
||
Bulk operations perform multiple write operations against one or more | ||
namespaces. Instead of making a call to the database for each operation, |
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.
S: since you are introducing the namespace
terminology, add a clause or sentence explaining what that means
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 also indicate that the bulk_write()
method is in the Client
class, which enables this feature against any namespace in the cluster
|
||
Bulk operations perform multiple write operations against one or more | ||
namespaces. Instead of making a call to the database for each operation, | ||
bulk operations perform multiple operations with one database call. |
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.
bulk operations perform multiple operations with one database call. | |
bulk operations perform multiple operations within one call to the database. |
- :ref:`bulk-sample-data` | ||
- :ref:`bulk-operation-types` | ||
- :ref:`bulk-modify-behavior` | ||
- :ref:`bulk-mixed-namespaces` | ||
- :ref:`bulk-addtl-info` |
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.
S: for multi-repo crosslinking and organization, make sure all anchors contain rust
To perform bulk write operations, ensure that your application meets the | ||
following requirements: | ||
|
||
- You are connected to MongoDB Server version 8.0 or later. | ||
- You are using {+driver-short+} version 3.0 or later. |
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.
S: indentation
- | The filter that matches one or more documents you want to update. When specified in an ``UpdateOneModel``, | ||
| only the first matching document will be updated. When specified in an ``UpdateManyModel``, all | ||
| matching documents will be updated. |
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.
I: remove the newline breaks |
from the beginning of each line in the paragraph
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.
Applies everywhere you might have done this
| Type: ``UpdateModifications`` | ||
|
||
* - ``array_filters`` | ||
- | (Optional) A set of filters specifying which array elements an update applies to. |
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.
- | (Optional) A set of filters specifying which array elements an update applies to. | |
- | (Optional) A set of filters specifying which array elements an update applies to if you are updating an array-valued field. |
let insert_many_result = mushrooms.insert_many(docs, None).await?; | ||
|
||
// begin-insert | ||
let mushrooms: Collection<Mushroom> = client.database("db").collection("mushrooms"); |
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.
S: reduce horizontal scroll (applies to all)
let mushrooms: Collection<Mushroom> = client.database("db").collection("mushrooms"); | |
let mushrooms: Collection<Mushroom> = client | |
.database("db") | |
.collection("mushrooms"); |
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.
A few more things but good work!
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to perform **bulk operations**. |
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.
In this guide, you can learn how to perform **bulk operations**. | |
In this guide, you can learn how to use the {+driver-short+} to perform **bulk operations**. |
Bulk operations perform multiple write operations against one or more | ||
namespaces, which refer to a database name and a collection name in | ||
``<database>.<collection>`` form. Since the bulk operation method is in | ||
the ``Client`` class, you can perform bulk operations against any namespace | ||
in your cluster. |
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.
S; use wording from the Server glossary
Bulk operations perform multiple write operations against one or more | |
namespaces, which refer to a database name and a collection name in | |
``<database>.<collection>`` form. Since the bulk operation method is in | |
the ``Client`` class, you can perform bulk operations against any namespace | |
in your cluster. | |
Bulk operations perform multiple write operations against one or more | |
**namespaces**. A namespace is a combination of the database name and the collection name, in the format | |
``<database>.<collection>``. Since you perform bulk operations on a | |
the ``Client`` instance, you can perform bulk operations against any namespace | |
in the cluster accessed by your client. |
Instead of making a call to the database for each operation, bulk operations | ||
perform multiple operations within one call to the 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.
S: right now, this sentence seems a little isolated, maybe you can make this a description of why one might use bulk ops.
Instead of making a call to the database for each operation, bulk operations | |
perform multiple operations within one call to the database. | |
You might perform a bulk operation to reduce the number of calls to the server. Instead of sending | |
a request for each operation, bulk operations perform multiple operations within one action. |
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.
lgtm with a few last comments! great work documenting this APi!
To perform a bulk insert operation, create an ``InsertOneModel`` instance for each document | ||
you want to insert. Then, pass a list of models to the ``bulk_write()`` method. | ||
|
||
To construct an ``InsertOneModel``, specify the following struct fields: |
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.
S: introduce this table (and in other sections) by clarifying that you set these fields by using the corresponding builder methods.
The following example sets the ``ordered`` field to ``false`` by chaining the ``ordered()`` method | ||
to the ``bulk_write()`` method: |
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.
The following example sets the ``ordered`` field to ``false`` by chaining the ``ordered()`` method | |
to the ``bulk_write()`` method: | |
The following example attempts to perform update and insert operations on the ``mushrooms`` collection | |
and sets the ``ordered`` field to ``false`` by chaining the ``ordered()`` method | |
to the ``bulk_write()`` method: |
The ``_id`` field is immutable and cannot be changed in an update operation. Since the ``UpdateOneModel`` | ||
includes instructions to update this field, the bulk operation generates a ``BulkWriteError`` and performs | ||
only an insert operation. If you do not set the ``ordered`` field to ``false``, the bulk operation will not | ||
attempt any subsequent operations after the unsuccessful update and will not insert the specified document. |
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.
The ``_id`` field is immutable and cannot be changed in an update operation. Since the ``UpdateOneModel`` | |
includes instructions to update this field, the bulk operation generates a ``BulkWriteError`` and performs | |
only an insert operation. If you do not set the ``ordered`` field to ``false``, the bulk operation will not | |
attempt any subsequent operations after the unsuccessful update and will not insert the specified document. | |
The ``_id`` field is immutable and cannot be changed in an update operation. Since the ``UpdateOneModel`` | |
includes instructions to update this field, the bulk operation returns a ``BulkWriteError`` and performs | |
only the insert operation. If you set the ``ordered`` field to ``true``, the driver does not | |
attempt any subsequent operations after the unsuccessful update operation, and the driver does not insert any documents. |
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.
Looks great, just a few suggestions. Also recommend considering adding a section about generating verbose results using this action method.
Bulk Operation Types | ||
-------------------- | ||
|
||
To perform a bulk write operation, pass an array of ``WriteModel`` struct instances to the |
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.
To perform a bulk write operation, pass an array of ``WriteModel`` struct instances to the | |
To perform a bulk write operation, pass an array of ``WriteModel`` enum instances to the |
| When set to ``true``, the driver does not perform subsequent write operations if one operation fails. | ||
| When set to ``false``, the driver continues to attempt write operations if one fails. |
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.
Very minor, but the actual continue-on-error behavior is done by the server, not the driver (for the most part). I'd remove mention of the driver here.
); | ||
// end-options | ||
|
||
// begin-mixed-namespaces |
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.
Up to you whether this would be worth the additional text, but this could be a good spot to make the collections generic over a non-Document
type and demonstrate how to use this helper method.
e.g.
#[derive(Serialize)]
struct Sweet {
name: String,
price: f32,
}
let sweets: Collection<Sweet> = client.database("ingredients").collection("sweets");
let sweet = Sweet {
name: "brown sugar".to_string(),
price: 3.99,
};
let insert_sweet_model = sweets.insert_one_model(sweet)?;
// ditto for dessert
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.
I added a section to Insert Operations instead that uses a Mushroom
struct and insert_one_model()
!
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.
Also added a section on bulk write return values, which includes a verbose_results()
explanation
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.
A few things, re-request if needed!
You can also use custom struct types to represent your sample data. For | ||
an example that performs a bulk operation using the ``Mushroom`` struct type, | ||
see the :ref:`bulk-insert-structs-example` on this page. |
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 also use custom struct types to represent your sample data. For | |
an example that performs a bulk operation using the ``Mushroom`` struct type, | |
see the :ref:`bulk-insert-structs-example` on this page. | |
You can also use custom struct types to represent your sample data. For | |
an example that performs a bulk operation by using ``Mushroom`` structs to model the same data, | |
see the :ref:`bulk-insert-structs-example` on this page. |
Insert Structs Example | ||
`````````````````````` | ||
|
||
You can also model your documents as structs and insert them into the ``db.mushrooms`` namespace. |
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 also model your documents as structs and insert them into the ``db.mushrooms`` namespace. | |
You can also model your documents by using structs and bulk insert struct instances | |
into the ``db.mushrooms`` namespace. |
The following code uses the ``insert_one_model()`` method to construct an ``InsertOneModel`` | ||
from each ``Mushroom`` instance: |
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.
The following code uses the ``insert_one_model()`` method to construct an ``InsertOneModel`` | |
from each ``Mushroom`` instance: | |
The following code uses the ``insert_one_model()`` method to construct an ``InsertOneModel`` | |
from each ``Mushroom`` instance, then inserts both models in a bulk operation: |
See Bulk Write Results | ||
---------------------- |
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.
See Bulk Write Results | |
---------------------- | |
Return Type | |
------------ |
- ``upserted_count``: the number of upserted documents | ||
- ``deleted_count``: the number of deleted documents | ||
|
||
You can also use the ``verbose_results()`` method to see more detailed information about each |
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 also use the ``verbose_results()`` method to see more detailed information about each | |
You can also use the ``verbose_results()`` method to see detailed information about each |
SummaryBulkWriteResult { inserted_count: 0, matched_count: 1, modified_count: 1, | ||
upserted_count: 0, deleted_count: 1 } |
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.
Q: if this example uses the verbose_results()
method, why is it printing a SummaryBulkWriteResult
instead of a VerboseBulkWriteResult
result?
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.
verbose_results()
returns a VerboseBulkWriteResult
, but the summary
field of the VerboseBulkWriteResult
(which is what I'm printing) has a value of SummaryBulkWriteResult
. Kinda confusing, here's the struct and its fields in the source code.
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.
Since this ends up giving you the same information as omitting the verbose_results() call, I changed it to print the update_results
and delete_results
fields instead
@@ -149,7 +201,7 @@ async fn main() -> mongodb::error::Result<()> { | |||
.build()), | |||
]; | |||
|
|||
let result = client.bulk_write(models).ordered(false).await?; | |||
let result = client.bulk_write(models).ordered(false).verbose_results().await?; |
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.
Q: why use verbose_results()
here when the operation results in an error?
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.
oops, didn't mean to add this here - removed
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.
looks great!
Pull Request Info
PR Reviewing Guidelines
JIRA - https://jira.mongodb.org/browse/DOCSP-39895
Staging - https://preview-mongodbnorareidy.gatsbyjs.io/rust/DOCSP-39895-bulk-write-guide/fundamentals/crud/write-operations/bulk/
Self-Review Checklist