You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{% note class="important" %}}Use of the bulkWrite methods is not recommended when connected to pre-2.6 MongoDB servers, as this was the first server version to support bulk write commands for insert, update, and delete in a way that allows the driver to implement the correct semantics for BulkWriteResult and BulkWriteException. The methods will still work for pre-2.6 servers, but performance will suffer, as each write operation has to be executed one at a time.{{% /note %}}
Copy file name to clipboardExpand all lines: Docs/reference/content/reference/driver/crud/writing.md
+44-11Lines changed: 44 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -189,20 +189,53 @@ For Replacing and Updating, `IsUpsert` can be specified such that, if the docume
189
189
190
190
## Bulk Writes
191
191
192
-
There are two types of bulk operations:
192
+
The [`BulkWriteAsync`]({{< apiref "M_MongoDB_Driver_IMongoCollection_1_BulkWriteAsync" >}}) method takes a variable number of [`WriteModel<>`]({{< apiref "T_MongoDB_Driver_WriteModel_1" >}}) instances and sends them to the server in the fewest possible number of batches. The size of a batch is limited by the maximum document size and each batch must consist of the same kind of write operations (i.e. deletes, inserts or updates).
193
193
194
-
1.**Ordered bulk operations.**
195
-
196
-
Executes all the operations in order and errors out on the first error.
194
+
For example, to run two delete operations with one call to the server:
197
195
198
-
1.**Unordered bulk operations.**
199
-
200
-
Executes all the operations and reports any errors. Unordered bulk operations do not guarantee the order of execution.
196
+
```csharp
197
+
varmodels=newWriteModel<BsonDocument>[]
198
+
{
199
+
newDeleteManyModel<BsonDocument>("{ x: 10 }"), // delete all documents where x == 10
200
+
newDeleteOneModel<BsonDocument>("{ x: 11 }") // delete 1 document where x == 11
201
+
};
202
+
203
+
awaitcollection.BulkWriteAsync(models);
204
+
```
201
205
202
-
Let’s look at two simple examples using ordered and unordered operations:
206
+
However, providing one insert and one delete would result in each getting sent in a different call to the server:
203
207
204
208
```csharp
209
+
varmodels=newWriteModel<BsonDocument>[]
210
+
{
211
+
newInsertOneModel<BsonDocument>("{ _id: 1}"),
212
+
newDeleteOneModel<BsonDocument>("{ x: 11 }") // delete 1 document where x == 11
213
+
};
214
+
215
+
awaitcollection.BulkWriteAsync(models); // will send one batch with the insert and one with the delete
Bulk writes can be ordered or unordered. The default is ordered.
234
+
235
+
1. Ordered bulk writes execute all the operations in order and error out on the first error.
236
+
1. Unordered bulk writes execute all the operations and report any errors at the end. Because the writes are unordered, the driver and/or server may re-order the operations in order to gain better performance.
{{% note class="important" %}}Use of the bulkWrite methods is not recommended when connected to pre-2.6 MongoDB servers, as this was the first server version to support bulk write commands for insert, update, and delete in a way that allows the driver to implement the correct semantics for BulkWriteResult and BulkWriteException. The methods will still work for pre-2.6 servers, but performance will suffer, as each write operation has to be executed one at a time.{{% /note %}}
260
+
{{% note class="important" %}}Use of the bulk write methods is not recommended when connected to pre-2.6 MongoDB servers, as this was the first server version to support bulk write commands for insert, update, and delete in a way that allows the driver to implement the correct semantics for BulkWriteResult and BulkWriteException. The methods will still work for pre-2.6 servers, but performance will suffer, as each write operation has to be executed one at a time.{{% /note %}}
0 commit comments