@@ -334,7 +334,7 @@ Examples
334
334
Bulk Write Operations
335
335
~~~~~~~~~~~~~~~~~~~~~
336
336
337
- The ``characters`` collection contains the following documents:
337
+ The ``characters`` collection in the ``guidebook`` database contains the following documents:
338
338
339
339
.. code-block:: javascript
340
340
@@ -348,43 +348,20 @@ operations on the collection:
348
348
.. code-block:: javascript
349
349
350
350
try {
351
- db.characters.bulkWrite(
352
- [
353
- { insertOne :
354
- {
355
- "document" :
356
- {
357
- "_id" : 4, "char" : "Dithras", "class" : "barbarian", "lvl" : 4
358
- }
359
- }
360
- },
361
- { insertOne :
362
- {
363
- "document" :
364
- {
365
- "_id" : 5, "char" : "Taeln", "class" : "fighter", "lvl" : 3
366
- }
367
- }
368
- },
369
- { updateOne :
370
- {
371
- "filter" : { "char" : "Eldon" },
372
- "update" : { $set : { "status" : "Critical Injury" } }
373
- }
374
- },
375
- { deleteOne :
376
- { "filter" : { "char" : "Brisbane"} }
377
- },
378
- { replaceOne :
379
- {
380
- "filter" : { "char" : "Meldane" },
381
- "replacement" : { "char" : "Tanys", "class" : "oracle", "lvl" : 4 }
382
- }
383
- }
384
- ]
385
- );
386
- }
387
- catch (e) {
351
+ db.characters.bulkWrite([
352
+ { insertOne: { "document": { "_id": 4, "char": "Dithras", "class": "barbarian", "lvl": 4 } } },
353
+ { insertOne: { "document": { "_id": 5, "char": "Taeln", "class": "fighter", "lvl": 3 } } },
354
+ { updateOne : {
355
+ "filter" : { "char" : "Eldon" },
356
+ "update" : { $set : { "status" : "Critical Injury" } }
357
+ } },
358
+ { deleteOne : { "filter" : { "char" : "Brisbane"} } },
359
+ { replaceOne : {
360
+ "filter" : { "char" : "Meldane" },
361
+ "replacement" : { "char" : "Tanys", "class" : "oracle", "lvl": 4 }
362
+ } }
363
+ ]);
364
+ } catch (e) {
388
365
print(e);
389
366
}
390
367
@@ -407,20 +384,23 @@ The operation returns the following:
407
384
}
408
385
}
409
386
410
- If the ``_id`` value for the second of the ``insertOne`` operations were a
411
- duplicate of an existing ``_id``, the following exception would be thrown:
387
+ If the collection had contained a document with ``"_id" : 5"``
388
+ before executing the bulk write, then when the bulk write is executed,
389
+ the following duplicate key exception would be thrown for the second insertOne:
412
390
413
391
.. code-block:: javascript
414
392
415
393
BulkWriteError({
416
394
"writeErrors" : [
417
395
{
418
- "index" : 0 ,
396
+ "index" : 1 ,
419
397
"code" : 11000,
420
- "errmsg" : "E11000 duplicate key error collection: guidebook.characters index: _id_ dup key: { : 4 }",
398
+ "errmsg" : "E11000 duplicate key error collection: guidebook.characters index: _id_ dup key: { _id: 5.0 }",
421
399
"op" : {
422
400
"_id" : 5,
423
- "char" : "Taeln"
401
+ "char" : "Taeln",
402
+ "class" : "fighter",
403
+ "lvl" : 3
424
404
}
425
405
}
426
406
],
@@ -431,9 +411,10 @@ duplicate of an existing ``_id``, the following exception would be thrown:
431
411
"nModified" : 0,
432
412
"nRemoved" : 0,
433
413
"upserted" : [ ]
434
- })
414
+ })
435
415
436
- Since ``ordered`` was true by default, only the first operation completes
416
+
417
+ Since ``ordered`` is true by default, only the first operation completes
437
418
successfully. The rest are not executed. Running the
438
419
:method:`~db.collection.bulkWrite()` with ``ordered : false`` would allow the
439
420
remaining operations to complete despite the error.
@@ -443,7 +424,7 @@ remaining operations to complete despite the error.
443
424
Unordered Bulk Write
444
425
~~~~~~~~~~~~~~~~~~~~
445
426
446
- The ``characters`` collection contains the following documents:
427
+ The ``characters`` collection in the ``guidebook`` database contains the following documents:
447
428
448
429
.. code-block:: javascript
449
430
@@ -458,44 +439,20 @@ the ``insertOne`` stages has a duplicate ``_id`` value:
458
439
.. code-block:: javascript
459
440
460
441
try {
461
- db.characters.bulkWrite(
462
- [
463
- { insertOne :
464
- {
465
- "document" :
466
- {
467
- "_id" : 4, "char" : "Dithras", "class" : "barbarian", "lvl" : 4
468
- }
469
- }
470
- },
471
- { insertOne :
472
- {
473
- "document" :
474
- {
475
- "_id" : 4, "char" : "Taeln", "class" : "fighter", "lvl" : 3
476
- }
477
- }
478
- },
479
- { updateOne :
480
- {
481
- "filter" : { "char" : "Eldon" },
482
- "update" : { $set : { "status" : "Critical Injury" } }
483
- }
484
- },
485
- { deleteOne :
486
- { "filter" : { "char" : "Brisbane"} }
487
- },
488
- { replaceOne :
489
- {
490
- "filter" : { "char" : "Meldane" },
491
- "replacement" : { "char" : "Tanys", "class" : "oracle", "lvl" : 4 }
492
- }
493
- }
494
- ],
495
- { ordered : false }
496
- );
497
- }
498
- catch (e) {
442
+ db.characters.bulkWrite([
443
+ { insertOne: { "document": { "_id": 4, "char": "Dithras", "class": "barbarian", "lvl": 4 } } },
444
+ { insertOne: { "document": { "_id": 4, "char": "Taeln", "class": "fighter", "lvl": 3 } } },
445
+ { updateOne : {
446
+ "filter" : { "char" : "Eldon" },
447
+ "update" : { $set : { "status" : "Critical Injury" } }
448
+ } },
449
+ { deleteOne : { "filter" : { "char" : "Brisbane"} } },
450
+ { replaceOne : {
451
+ "filter" : { "char" : "Meldane" },
452
+ "replacement" : { "char" : "Tanys", "class" : "oracle", "lvl": 4 }
453
+ } }
454
+ ], { ordered : false } );
455
+ } catch (e) {
499
456
print(e);
500
457
}
501
458
@@ -506,12 +463,14 @@ The operation returns the following:
506
463
BulkWriteError({
507
464
"writeErrors" : [
508
465
{
509
- "index" : 0 ,
466
+ "index" : 1 ,
510
467
"code" : 11000,
511
- "errmsg" : "E11000 duplicate key error collection: guidebook.characters index: _id_ dup key: { : 4 }",
468
+ "errmsg" : "E11000 duplicate key error collection: guidebook.characters index: _id_ dup key: { _id : 4.0 }",
512
469
"op" : {
513
470
"_id" : 4,
514
- "char" : "Taeln"
471
+ "char" : "Taeln",
472
+ "class" : "fighter",
473
+ "lvl" : 3
515
474
}
516
475
}
517
476
],
@@ -563,7 +522,7 @@ operations on the collection using a :ref:`write concern <wc-w>` value of
563
522
"update" : { $inc : { "encounter" : -0.25 } }
564
523
},
565
524
},
566
- { deleteMany : { "filter" : { "encounter" { $lt : 0 } } } },
525
+ { deleteMany : { "filter" : { "encounter": { $lt : 0 } } } },
567
526
{ insertOne :
568
527
{
569
528
"document" :
@@ -575,8 +534,7 @@ operations on the collection using a :ref:`write concern <wc-w>` value of
575
534
],
576
535
{ writeConcern : { w : "majority", wtimeout : 100 } }
577
536
);
578
- }
579
- catch (e) {
537
+ } catch (e) {
580
538
print(e);
581
539
}
582
540
@@ -592,10 +550,27 @@ has passed.
592
550
"writeConcernErrors" : [
593
551
{
594
552
"code" : 64,
553
+ "codeName" : "WriteConcernFailed",
554
+ "errmsg" : "waiting for replication timed out",
595
555
"errInfo" : {
596
556
"wtimeout" : true
597
- },
598
- "errmsg" : "waiting for replication timed out"
557
+ }
558
+ },
559
+ {
560
+ "code" : 64,
561
+ "codeName" : "WriteConcernFailed",
562
+ "errmsg" : "waiting for replication timed out",
563
+ "errInfo" : {
564
+ "wtimeout" : true
565
+ }
566
+ },
567
+ {
568
+ "code" : 64,
569
+ "codeName" : "WriteConcernFailed",
570
+ "errmsg" : "waiting for replication timed out",
571
+ "errInfo" : {
572
+ "wtimeout" : true
573
+ }
599
574
}
600
575
],
601
576
"nInserted" : 1,
@@ -604,7 +579,7 @@ has passed.
604
579
"nModified" : 4,
605
580
"nRemoved" : 1,
606
581
"upserted" : [ ]
607
- })
582
+ })
608
583
609
584
The result set shows the operations executed since
610
585
``writeConcernErrors`` errors are *not* an indicator that any write
0 commit comments