Skip to content

Commit 65d2fbb

Browse files
authored
DOCSP-32423 BulkWriteResult (#4410)
* DOCSP-32423 BulkWriteResult * correct fields * new sections for info specific to exception * clean-up
1 parent 2a223f1 commit 65d2fbb

File tree

3 files changed

+111
-88
lines changed

3 files changed

+111
-88
lines changed

source/reference/method/Bulk.execute.txt

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ Bulk.execute()
1010
:depth: 1
1111
:class: singlecol
1212

13-
.. include:: /includes/fact-bulkwrite.rst
13+
.. tip::
14+
15+
MongoDB also provides :method:`db.collection.bulkWrite()` for
16+
performing bulk write operations.
1417

1518
Description
1619
-----------
@@ -28,15 +31,11 @@ Description
2831
:widths: 20 20 80
2932

3033
* - Parameter
31-
3234
- Type
33-
3435
- Description
3536

3637
* - ``writeConcern``
37-
3838
- document
39-
4039
- Optional. :ref:`write concern <write-concern>`
4140
document for the bulk operation as a whole. Omit to use
4241
default. For a standalone :binary:`~bin.mongod` server, the
@@ -53,8 +52,6 @@ Description
5352

5453

5554

56-
57-
5855
:returns: A :method:`BulkWriteResult` object that contains the
5956
status of the operation.
6057

@@ -122,14 +119,17 @@ The operation returns the following :method:`BulkWriteResult()` object:
122119
.. code-block:: javascript
123120

124121
BulkWriteResult({
125-
"writeErrors" : [ ],
126-
"writeConcernErrors" : [ ],
127-
"nInserted" : 2,
128-
"nUpserted" : 0,
129-
"nMatched" : 0,
130-
"nModified" : 0,
131-
"nRemoved" : 0,
132-
"upserted" : [ ]
122+
acknowledged: true,
123+
insertedCount: 2,
124+
insertedIds: {
125+
'0': ObjectId("64e61e3b84ff8808cd43a92c"),
126+
'1': ObjectId("64e61e3b84ff8808cd43a92d")
127+
},
128+
matchedCount: 0,
129+
modifiedCount: 0,
130+
deletedCount: 0,
131+
upsertedCount: 0,
132+
upsertedIds: {}
133133
})
134134

135135
For details on the return object, see :method:`BulkWriteResult()`. For
@@ -140,11 +140,11 @@ details on the batches executed, see :method:`Bulk.getOperations()`.
140140
Override Default Write Concern
141141
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142142

143-
The following operation to a replica set specifies a :doc:`write
144-
concern </reference/write-concern>` of ``"w: 1"`` with a
145-
``wtimeout`` of 5000 milliseconds such that the method returns after
146-
the writes propagate to a majority of the voting replica set members or
147-
the method times out after 5 seconds.
143+
The following operation to a replica set specifies a :ref:`write
144+
concern <write-concern>` of ``"w: 1"`` with a ``wtimeout`` of 5000
145+
milliseconds such that the method returns after the writes propagate
146+
to a majority of the voting replica set members or the method times out
147+
after five seconds.
148148

149149
.. code-block:: javascript
150150

@@ -158,14 +158,17 @@ The operation returns the following :method:`BulkWriteResult()` object:
158158
.. code-block:: javascript
159159

160160
BulkWriteResult({
161-
"writeErrors" : [ ],
162-
"writeConcernErrors" : [ ],
163-
"nInserted" : 2,
164-
"nUpserted" : 0,
165-
"nMatched" : 0,
166-
"nModified" : 0,
167-
"nRemoved" : 0,
168-
"upserted" : [ ]
161+
acknowledged: true,
162+
insertedCount: 2,
163+
insertedIds: {
164+
'0': ObjectId("64e61e3b84ff8808cd43a92c"),
165+
'1': ObjectId("64e61e3b84ff8808cd43a92d")
166+
},
167+
matchedCount: 0,
168+
modifiedCount: 0,
169+
deletedCount: 0,
170+
upsertedCount: 0,
171+
upsertedIds: {}
169172
})
170173

171174
.. see::

source/reference/method/Bulk.insert.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Insert Inaccuracies
4747

4848
.. include:: /includes/fact-insert-inaccuracies.rst
4949

50-
.. |writeResult| replace:: :data:`BulkWriteResult.nInserted`
50+
.. |writeResult| replace:: :data:`BulkWriteResult.insertedCount`
5151

5252
Example
5353
-------

source/reference/method/BulkWriteResult.txt

Lines changed: 79 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,110 +20,130 @@ BulkWriteResult()
2020
Properties
2121
----------
2222

23-
The :method:`BulkWriteResult` has the following properties:
23+
``BulkWriteResult`` has the following properties:
2424

25-
.. data:: BulkWriteResult.nInserted
25+
.. data:: BulkWriteResult.acknowledged
2626

27-
The number of documents inserted using the :method:`Bulk.insert()`
28-
method. For documents inserted through operations with the
29-
:method:`Bulk.find.upsert()` option, see the
30-
:data:`~BulkWriteResult.nUpserted` field instead.
27+
Boolean indicating whether or not the bulk write was acknowledged.
3128

32-
.. data:: BulkWriteResult.nMatched
29+
.. data:: BulkWriteResult.deletedCount
3330

34-
The number of existing documents selected for update or replacement.
35-
If the update/replacement operation results in no change to an
36-
existing document, e.g. :update:`$set` expression updates the value
37-
to the current value, :data:`~BulkWriteResult.nMatched` can be
38-
greater than :data:`~BulkWriteResult.nModified`.
31+
Count of deleted documents.
3932

40-
.. data:: BulkWriteResult.nModified
33+
.. data:: BulkWriteResult.insertedCount
4134

42-
The number of existing documents updated or replaced. If the
43-
update/replacement operation results in no change to an existing
44-
document, such as setting the value of the field to its current
45-
value, :data:`~BulkWriteResult.nModified` can be less than
46-
:data:`~BulkWriteResult.nMatched`. Inserted documents do not affect
47-
the number of :data:`~BulkWriteResult.nModified`; refer to the
48-
:data:`~BulkWriteResult.nInserted` and
49-
:data:`~BulkWriteResult.nUpserted` fields instead.
35+
Count of inserted documents.
5036

51-
.. data:: BulkWriteResult.nRemoved
37+
.. data:: BulkWriteResult.insertedIds
5238

53-
The number of documents removed.
39+
.. list-table::
40+
:header-rows: 1
41+
:widths: 10 45 45
5442

55-
.. data:: BulkWriteResult.nUpserted
43+
* - ``mongosh`` Version
44+
- Description
45+
- Example
5646

57-
The number of documents inserted through operations with the
58-
:method:`Bulk.find.upsert()` option.
47+
* - v1.x
48+
- Array of inserted documents by ``ObjectId``.
49+
- ``[ ObjectId(...), ObjectId(...), ObjectId(...) ]``
5950

60-
.. data:: BulkWriteResult.upserted
51+
* - v2.x
52+
- Map from each inserted document index to its ``ObjectId``.
53+
- ``{ 0: ObjectId(...), 1: ObjectId(...), 2: ObjectId(...) }```
6154

62-
An array of documents that contains information for each document
63-
inserted through operations with the :method:`Bulk.find.upsert()`
64-
option.
55+
.. data:: BulkWriteResult.matchedCount
6556

66-
Each document contains the following information:
57+
Count of matched documents.
6758

68-
.. data:: BulkWriteResult.upserted.index
59+
.. data:: BulkWriteResult.modifiedCount
6960

70-
An integer that identifies the operation in the
71-
bulk operations list, which uses a zero-based index.
61+
Count of modified documents.
7262

73-
.. data:: BulkWriteResult.upserted._id
63+
.. data:: BulkWriteResult.upsertedCount
7464

75-
The ``_id`` value of the inserted document.
65+
Count of upserted documents.
7666

77-
.. data:: BulkWriteResult.writeErrors
67+
.. data:: BulkWriteResult.upsertedIds
68+
69+
.. list-table::
70+
:header-rows: 1
71+
:widths: 10 45 45
72+
73+
* - ``mongosh`` Version
74+
- Description
75+
- Example
76+
77+
* - v1.x
78+
- Array of upserted documents by ``ObjectId``.
79+
- ``[ ObjectId(...), ObjectId(...), ObjectId(...) ]``
80+
81+
* - v2.x
82+
- Map from each upserted document index to its ``ObjectId``.
83+
- ``{ 0: ObjectId(...), 1: ObjectId(...), 2: ObjectId(...) }```
84+
85+
writeErrors Exception
86+
---------------------
87+
88+
If there is a write error while processing the bulk write,
89+
``mongosh`` raises an exception that contains a ``writeErrors``
90+
property with the following fields:
91+
92+
.. data:: writeErrors
7893

7994
An array of documents that contains information regarding any error,
8095
unrelated to write concerns, encountered during the update
81-
operation. The :data:`~BulkWriteResult.writeErrors` array contains
82-
an error document for each write operation that errors.
96+
operation. ``writeErrors`` contains an error document for each write
97+
operation that errors.
8398

8499
Each error document contains the following fields:
85100

86-
.. data:: BulkWriteResult.writeErrors.index
101+
.. data:: writeErrors.index
87102

88-
An integer that identifies the write operation in the bulk
103+
Integer that identifies the write operation in the bulk
89104
operations list, which uses a zero-based index. See also
90105
:method:`Bulk.getOperations()`.
91106

92-
.. data:: BulkWriteResult.writeErrors.code
107+
.. data:: writeErrors.code
93108

94-
An integer value identifying the error.
109+
Integer value identifying the error.
95110

96-
.. data:: BulkWriteResult.writeErrors.errmsg
111+
.. data:: writeErrors.errmsg
97112

98-
A description of the error.
113+
Description of the error.
99114

100-
.. data:: BulkWriteResult.writeErrors.op
115+
.. data:: writeErrors.op
101116

102117
A document identifying the operation that failed. For instance,
103-
an update/replace operation error will return a document
104-
specifying the query, the update, the ``multi`` and the
105-
``upsert`` options; an insert operation will return the document
118+
an update or replace operation error will return a document
119+
specifying the query, the update, the ``multi``, and the
120+
``upsert`` options. An insert opertation returns the document
106121
the operation tried to insert.
107122

108-
.. data:: BulkWriteResult.writeConcernError
123+
writeConcernError Exception
124+
---------------------------
125+
126+
If there is a write concern error while processing the bulk write,
127+
``mongosh`` raises an exception that contains a ``writeConcernError``
128+
property with the following fields:
109129

110-
Document that describe error related to write concern and contains
111-
the field:
130+
.. data:: writeConcernError
112131

113-
.. data:: BulkWriteResult.writeConcernError.code
132+
Document that describes the error related to write concern and
133+
contains the fields:
114134

115-
An integer value identifying the cause of the write concern error.
135+
.. data:: writeConcernError.code
116136

117-
.. data:: BulkWriteResult.writeConcernError.errmsg
137+
Integer value identifying the cause of the write concern error.
118138

119-
A description of the cause of the write concern error.
139+
.. data:: writeConcernError.errmsg
120140

121-
.. data:: BulkWriteResult.writeConcernError.errInfo.writeConcern
141+
Description of the cause of the write concern error.
122142

123-
.. versionadded:: 4.4
143+
.. data:: writeConcernError.errInfo.writeConcern
124144

125145
.. include:: /includes/fact-errInfo-wc.rst
126146

127-
.. data:: BulkWriteResult.writeConcernError.errInfo.writeConcern.provenance
147+
.. data:: writeConcernError.errInfo.writeConcern.provenance
128148

129149
.. include:: /includes/fact-errInfo-wc-provenance.rst

0 commit comments

Comments
 (0)