@@ -52,11 +52,11 @@ The ``WriteError`` type contains the following methods:
52
52
The `BulkWriteError <{+core-api+}/bulk/BulkWriteError.html>`__ type extends
53
53
``WriteError`` and inherits the above methods. This type is used for bulk
54
54
operations and includes an ``index`` field that identifies the item in the bulk
55
- operation that led to the error.
55
+ operation that caused the error.
56
56
57
57
The `WriteConcernError <{+core-api+}/bulk/WriteConcernError.html>`__ is not
58
- related to the ``WriteError`` type and details errors relating to the write
59
- concern. In addition to the ``getCode``, ``getMessage``, and ``getDetails``
58
+ related to the ``WriteError`` type and details errors relating to the :ref:` write
59
+ concern </reference/write-concern/>` . In addition to the ``getCode``, ``getMessage``, and ``getDetails``
60
60
methods, ``WriteConcernError`` has the ``getCodeName`` method, which returns the
61
61
name associated with the error code. This type does not have the ``getCategory``
62
62
method.
@@ -65,35 +65,40 @@ Write Error
65
65
~~~~~~~~~~~
66
66
67
67
The driver raises a ``WriteError`` error for any errors that it
68
- encounters when performing a write operation that are not related to
69
- satisfying the write concern. Because there are many causes for this
70
- error, the ``WriteError`` type contains fields that describe the type of
71
- write error and reason for the error.
72
-
73
- For example, the driver raises a ``WriteError`` error if you attempt to
74
- insert a document into a collection that violates the collection's
75
- schema validation rules. Suppose the collection has a rule where the
76
- value of the ``quantity`` field must be an ``int`` type. If you
77
- attempt to insert a document where the value of ``quantity`` is
78
- ``"three"``, the driver prints the following error message:
68
+ encounters when performing single write operations that are not related to
69
+ satisfying the write concern.
70
+
71
+ For example, the driver raises a ``WriteError`` error if you attempt to insert a
72
+ document into a collection with an ObjectId value that already exists in the
73
+ collection. Suppose the collection has an existing document with an ``ObjectId`` of `1`. If you attempt to insert a document
74
+ where the value of ``ObjectId`` is ``1``, the driver prints the following
75
+ error message:
79
76
80
77
.. code-block:: none
81
78
:copyable: false
82
- :emphasize-lines: 2-3
83
-
84
- Error: Error { kind: Write(WriteError(WriteError { code: 121, code_name:
85
- None, message: "Document failed validation", details:
86
- Some(Document({"failingDocumentId": Int32(1), "details":
87
- Document({"operatorName": String("$jsonSchema"), "title":
88
- String("Numerical Validation"), "schemaRulesNotSatisfied":
89
- Array(...)})})) })), labels: {},
90
- wire_version: None, source: None }
91
-
92
- In the preceding error message, the ``message`` field describes the
93
- reason for the error, and the ``details`` field provides specific
94
- details about the failing operation. To address this error, you must
95
- either revise the document to adhere to the schema validation rules or
96
- bypass validation.
79
+
80
+ WriteError: { code: 11000, message: "E11000 duplicate key error collection:
81
+ testDB.testCollection index: _id_ dup key: { _id: 1 }", details: {} }
82
+
83
+ In the preceding error message, the ``message`` field describes the reason for
84
+ the error, and the ``details`` field provides specific details about the failing
85
+ operation. To address this error, you must revise the document to have a unique
86
+ ``ObjectId``.
87
+
88
+ Bulk Write Error
89
+ ----------------
90
+
91
+ If this same example was run in during bulk write operation, an additional field
92
+ would be added to the error detailing the index of the item with the duplicate
93
+ ``ObjectId``
94
+
95
+ .. code-block:: none
96
+ :copyable: false
97
+ :emphasize-lines: 1
98
+
99
+ BulkWriteError{index=2, code=11000, message='E11000 duplicate key error
100
+ collection: testDB.testCollection index: _id_ dup key: { _id: 1 }',
101
+ details=Document{{}}}
97
102
98
103
Write Concern Error
99
104
~~~~~~~~~~~~~~~~~~~
0 commit comments