Skip to content

Commit c8fd232

Browse files
author
Kay Kim
committed
DOCSP-4249: small bulkWrite example fixes
1 parent bbebdd2 commit c8fd232

File tree

1 file changed

+68
-93
lines changed

1 file changed

+68
-93
lines changed

source/reference/method/db.collection.bulkWrite.txt

Lines changed: 68 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ Examples
334334
Bulk Write Operations
335335
~~~~~~~~~~~~~~~~~~~~~
336336

337-
The ``characters`` collection contains the following documents:
337+
The ``characters`` collection in the ``guidebook`` database contains the following documents:
338338

339339
.. code-block:: javascript
340340

@@ -348,43 +348,20 @@ operations on the collection:
348348
.. code-block:: javascript
349349

350350
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) {
388365
print(e);
389366
}
390367

@@ -407,20 +384,23 @@ The operation returns the following:
407384
}
408385
}
409386

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

413391
.. code-block:: javascript
414392

415393
BulkWriteError({
416394
"writeErrors" : [
417395
{
418-
"index" : 0,
396+
"index" : 1,
419397
"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 }",
421399
"op" : {
422400
"_id" : 5,
423-
"char" : "Taeln"
401+
"char" : "Taeln",
402+
"class" : "fighter",
403+
"lvl" : 3
424404
}
425405
}
426406
],
@@ -431,9 +411,10 @@ duplicate of an existing ``_id``, the following exception would be thrown:
431411
"nModified" : 0,
432412
"nRemoved" : 0,
433413
"upserted" : [ ]
434-
})
414+
})
435415

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
437418
successfully. The rest are not executed. Running the
438419
:method:`~db.collection.bulkWrite()` with ``ordered : false`` would allow the
439420
remaining operations to complete despite the error.
@@ -443,7 +424,7 @@ remaining operations to complete despite the error.
443424
Unordered Bulk Write
444425
~~~~~~~~~~~~~~~~~~~~
445426

446-
The ``characters`` collection contains the following documents:
427+
The ``characters`` collection in the ``guidebook`` database contains the following documents:
447428

448429
.. code-block:: javascript
449430

@@ -458,44 +439,20 @@ the ``insertOne`` stages has a duplicate ``_id`` value:
458439
.. code-block:: javascript
459440

460441
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) {
499456
print(e);
500457
}
501458

@@ -506,12 +463,14 @@ The operation returns the following:
506463
BulkWriteError({
507464
"writeErrors" : [
508465
{
509-
"index" : 0,
466+
"index" : 1,
510467
"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 }",
512469
"op" : {
513470
"_id" : 4,
514-
"char" : "Taeln"
471+
"char" : "Taeln",
472+
"class" : "fighter",
473+
"lvl" : 3
515474
}
516475
}
517476
],
@@ -563,7 +522,7 @@ operations on the collection using a :ref:`write concern <wc-w>` value of
563522
"update" : { $inc : { "encounter" : -0.25 } }
564523
},
565524
},
566-
{ deleteMany : { "filter" : { "encounter" { $lt : 0 } } } },
525+
{ deleteMany : { "filter" : { "encounter": { $lt : 0 } } } },
567526
{ insertOne :
568527
{
569528
"document" :
@@ -575,8 +534,7 @@ operations on the collection using a :ref:`write concern <wc-w>` value of
575534
],
576535
{ writeConcern : { w : "majority", wtimeout : 100 } }
577536
);
578-
}
579-
catch (e) {
537+
} catch (e) {
580538
print(e);
581539
}
582540

@@ -592,10 +550,27 @@ has passed.
592550
"writeConcernErrors" : [
593551
{
594552
"code" : 64,
553+
"codeName" : "WriteConcernFailed",
554+
"errmsg" : "waiting for replication timed out",
595555
"errInfo" : {
596556
"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+
}
599574
}
600575
],
601576
"nInserted" : 1,
@@ -604,7 +579,7 @@ has passed.
604579
"nModified" : 4,
605580
"nRemoved" : 1,
606581
"upserted" : [ ]
607-
})
582+
})
608583

609584
The result set shows the operations executed since
610585
``writeConcernErrors`` errors are *not* an indicator that any write

0 commit comments

Comments
 (0)