Skip to content

Commit b5c346e

Browse files
committed
CSHARP-1377: rewrote the bulk write reference documentation.
1 parent 9b2a203 commit b5c346e

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

Docs/reference/content/getting_started/quick_tour.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,10 @@ var models = new WriteModel<BsonDocument>[]
321321
};
322322

323323
// 1. Ordered bulk operation - order of operation is guaranteed
324-
await collection.BulkWrite(models);
324+
await collection.BulkWriteAsync(models);
325325

326326
// 2. Unordered bulk operation - no guarantee of order of operation
327-
await collection.BulkWrite(models, new BulkWriteOptions { IsOrdered = false });
327+
await collection.BulkWriteAsync(models, new BulkWriteOptions { IsOrdered = false });
328328
```
329329

330330
{{% 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 %}}

Docs/reference/content/reference/driver/crud/writing.md

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,53 @@ For Replacing and Updating, `IsUpsert` can be specified such that, if the docume
189189

190190
## Bulk Writes
191191

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).
193193

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:
197195

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+
var models = new WriteModel<BsonDocument>[]
198+
{
199+
new DeleteManyModel<BsonDocument>("{ x: 10 }"), // delete all documents where x == 10
200+
new DeleteOneModel<BsonDocument>("{ x: 11 }") // delete 1 document where x == 11
201+
};
202+
203+
await collection.BulkWriteAsync(models);
204+
```
201205

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:
203207

204208
```csharp
209+
var models = new WriteModel<BsonDocument>[]
210+
{
211+
new InsertOneModel<BsonDocument>("{ _id: 1}"),
212+
new DeleteOneModel<BsonDocument>("{ x: 11 }") // delete 1 document where x == 11
213+
};
214+
215+
await collection.BulkWriteAsync(models); // will send one batch with the insert and one with the delete
216+
```
217+
218+
219+
### Write Models
220+
221+
There are 6 types of write models:
222+
223+
1. [`InsertOneModel`]({{< apiref "T_MongoDB_Driver_InsertOneModel_1" >}})
224+
1. [`DeleteOneModel`]({{< apiref "T_MongoDB_Driver_DeleteOneModel_1" >}})
225+
1. [`DeleteManyModel`]({{< apiref "T_MongoDB_Driver_DeleteManyModel_1" >}})
226+
1. [`UpdateOneModel`]({{< apiref "T_MongoDB_Driver_UpdateOneModel_1" >}})
227+
1. [`UpdateManyModelModel`]({{< apiref "T_MongoDB_Driver_UpdateManyModel_1" >}})
228+
1. [`ReplaceOneModel`]({{< apiref "T_MongoDB_Driver_ReplaceOneModel_1" >}})
205229

230+
231+
### Ordered and Unordered
232+
233+
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.
237+
238+
```csharp
206239
var models = new WriteModel<BsonDocument>[]
207240
{
208241
new InsertOneModel<BsonDocument>(new BsonDocument("_id", 4)),
@@ -218,10 +251,10 @@ var models = new WriteModel<BsonDocument>[]
218251
};
219252

220253
// 1. Ordered bulk operation - order of operation is guaranteed
221-
await collection.BulkWrite(models);
254+
await collection.BulkWriteAsync(models);
222255

223256
// 2. Unordered bulk operation - no guarantee of order of operation
224-
await collection.BulkWrite(models, new BulkWriteOptions { IsOrdered = false });
257+
await collection.BulkWriteAsync(models, new BulkWriteOptions { IsOrdered = false });
225258
```
226259

227-
{{% 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

Comments
 (0)