1
1
.. _java-operation-errors:
2
2
3
- ============================
4
- Troubleshooting Write Errors
5
- ============================
3
+ ================================
4
+ Troubleshooting Write Exceptions
5
+ ================================
6
6
7
7
.. meta::
8
8
:description: Understand how to handle write exceptions in the MongoDB Java Sync Driver, including error types like WriteError and BulkWriteError.
@@ -35,7 +35,7 @@ appropriate actions to either handle them or correct the error-causing code.
35
35
discussions, or general technical support
36
36
37
37
Write Error
38
- -----------
38
+ ~~~~~~~~~~~
39
39
40
40
If the driver encounters an error while performing a write operation, it
41
41
creates an error of the `WriteError <{+core-api+}/WriteError.html>`__ type.
@@ -51,10 +51,10 @@ The ``WriteError`` type contains the following fields:
51
51
Write Exception
52
52
---------------
53
53
54
- The driver raises a `MongoWriteException <{+core-api+}/MongoWriteException.html>` for
54
+ The driver raises a `MongoWriteException <{+core-api+}/MongoWriteException.html>`__ for
55
55
any errors that it encounters when performing single write operations that are
56
- not related to satisfying the write concern. The ``WriteException `` type
57
- contains the ``WriteError`` object and a ``ServerAddress`` object .
56
+ not related to satisfying the write concern. The ``MongoWriteException `` type
57
+ contains the ``WriteError`` object.
58
58
59
59
For example, the driver raises a ``MongoWriteException`` if you attempt to
60
60
insert a document into a collection that violates the collection's
@@ -65,7 +65,7 @@ attempt to insert a document where the value of ``quantity`` is
65
65
66
66
.. code-block:: none
67
67
:copyable: false
68
- :emphasize-lines: 1, 4-6
68
+ :emphasize-lines: 1, 4-7
69
69
70
70
Exception in thread "main" com.mongodb.MongoWriteException: Document failed validation at
71
71
com.mongodb.internal.connection.ProtocolHelper.getWriteException(ProtocolHelper.java:228)
@@ -94,17 +94,25 @@ as follows:
94
94
95
95
.. code-block:: none
96
96
:copyable: false
97
- :emphasize-lines: 1, 5, 9
97
+ :emphasize-lines: 1-2, 6-9, 13-16
98
98
99
- Exception in thread "main" com.mongodb.MongoBulkWriteException: Bulk write operation result had errors
100
- at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:258)
101
- ...
102
- at BulkWriteMultipleValidationErrorsExample.main(BulkWriteMultipleValidationErrorsExample.java:30)
103
- Caused by: com.mongodb.MongoWriteException: WriteError{code=121, message='Document failed validation', details={ operator: "$jsonSchema", schemaRules: { bsonType: "int", description: "must be an integer" }, offendingDocument: {"name":"Apple","quantity":"three"} }}
104
- at com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
105
- at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
106
- ... 19 more
107
- Caused by: com.mongodb.MongoWriteException: WriteError{code=121, message='Document failed validation', details={ operator: "$jsonSchema", schemaRules: { bsonType: "int", description: "must be an integer" }, offendingDocument: {"name":"Banana","quantity":"ten"} }}
108
- at com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
109
- at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
99
+ Exception in thread "main" com.mongodb.MongoBulkWriteException: Bulk write
100
+ operation result had errors at
101
+ com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:258)
102
+ ... at
103
+ BulkWriteMultipleValidationErrorsExample.main(BulkWriteMultipleValidationErrorsExample.java:30)
104
+ Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
105
+ message='Document failed validation', details={ operator: "$jsonSchema",
106
+ schemaRules: { bsonType: "int", description: "must be an integer" },
107
+ offendingDocument: {"name":"Apple","quantity":"three"} }} at
108
+ com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
109
+ at
110
+ com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
111
+ ... 19 more Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
112
+ message='Document failed validation', details={ operator: "$jsonSchema",
113
+ schemaRules: { bsonType: "int", description: "must be an integer" },
114
+ offendingDocument: {"name":"Banana","quantity":"ten"} }} at
115
+ com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
116
+ at
117
+ com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
110
118
... 19 more
0 commit comments