Skip to content

DOCSP-30350 Java Write Exception Troubleshooting #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7de199d
DOCSP-30350 Java Write Error Page
mballard-mdb Jun 11, 2025
51772d1
DOCSP-30350 add to toc tree and fix links
mballard-mdb Jun 11, 2025
ede2dcd
DOCSP-30350 toc tree update and editing
mballard-mdb Jun 11, 2025
e657ce8
DOCSP-30350 code blocks and update write/bulkwrite sections
mballard-mdb Jun 11, 2025
8341e97
DOCSP-30350 Updating grammar and Write/bulk Write error messages
mballard-mdb Jun 11, 2025
1d219ee
DOCSP30350 fix code example format
mballard-mdb Jun 11, 2025
d60da9e
DOCSP-30350 code examples and additional getCategory information
mballard-mdb Jun 11, 2025
2076d6b
DOCSP-30350 fixing up minor issues with page, final check
mballard-mdb Jun 11, 2025
402ce2e
DOCSP30350 add exceptions not just errors to doc
mballard-mdb Jun 11, 2025
11785f5
DOCSP-30350 fixing formatting
mballard-mdb Jun 11, 2025
32d55d6
DOCSP-30350 Write exceptions, reorg
mballard-mdb Jun 11, 2025
ff68f50
DOCSP-30350 reorg again and edit writing on some descriptions
mballard-mdb Jun 11, 2025
e30b2ed
DOCSP30350- add quotes on strings
mballard-mdb Jun 11, 2025
e6fc4b6
DOCSP-30350 toc tree naming update
mballard-mdb Jun 11, 2025
28c2901
DOCSP-30350 add link to schema validation page
mballard-mdb Jun 11, 2025
ab053b6
DOCSP-30350 schema validation link fix
mballard-mdb Jun 11, 2025
d4c425f
DOCSP-30350 put troubleshooting into main pages, not separate
mballard-mdb Jun 12, 2025
0bead5f
DOCSP-30350 Adding descriptions and editing
mballard-mdb Jun 12, 2025
2afc1d1
DOCSP-30350 fixing example headings and final edits
mballard-mdb Jun 12, 2025
2922c97
DOCSP-30350 making vocab consistent, active voice
mballard-mdb Jun 12, 2025
140c02d
DOCSP-30350 fix typo
mballard-mdb Jun 12, 2025
763949d
DOCSP-30350 adding suggestions and API doc links
mballard-mdb Jun 13, 2025
2b7584f
DOCSP-30350 small tweaks to punctuation and wording
mballard-mdb Jun 13, 2025
cc021ca
DOCSP-30350 final edits
mballard-mdb Jun 13, 2025
dd420c2
DOCSP-30350 add schema val link to bulk
mballard-mdb Jun 13, 2025
4abbd49
DOCSP-30350 minor change tech reivew clarify schema validation
mballard-mdb Jun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions source/crud/bulk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,69 @@ Even though the write operation inserting a document with a duplicate key result
in an error, the other operations are executed because the write operation is
unordered.

.. _java-bulk-insert-troubleshooting:

Troubleshooting
---------------

.. include:: /includes/crud/write-error.rst

Bulk Write Exception
~~~~~~~~~~~~~~~~~~~~

The driver creates a write exception when it creates a ``WriteError`` object. If an
error is detected during a bulk write operation, the driver throws a `MongoBulkWriteException
<{+core-api+}/MongoBulkWriteException.html>`__.

A ``MongoBulkWriteException`` contains a ``writeErrors`` field consisting of a
list of one or more ``WriteError`` objects associated with the same bulk write
operation.

Example
```````

For example, the driver throws a ``MongoBulkWriteException`` if your bulk insert
contains two documents that violate the collection's schema
validation rules. Suppose the collection has a rule where the value of the
``quantity`` field must be an ``int`` type. If your bulk insert contains a
document with a ``quantity`` of ``"three"`` and another with a ``quantity`` of
``"ten"``, the driver prints the following error message:

.. code-block:: none
:copyable: false
:emphasize-lines: 1-2, 6-9, 13-16

Exception in thread "main" com.mongodb.MongoBulkWriteException: Bulk write
operation result had errors at
com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:258)
... at
BulkWriteMultipleValidationErrorsExample.main(BulkWriteMultipleValidationErrorsExample.java:30)
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"} }} at
com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
... 19 more
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"} }} at
com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
at
com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
... 19 more


In the preceding error message, the ``MongoBulkWriteException`` object provides
a vague description of the error, as documents associated with the same bulk
operation could produce different error types. Instead, refer to the
``WriteError`` objects' ``message`` fields to determine the cause of each error.
Refer to the ``WriteError`` objects' ``details`` field for more specific
information, such as the schema rules and the offending documents. To address
this error, you must either revise the document to adhere to the schema
validation rules or bypass validation.

Additional Information
----------------------

Expand Down
48 changes: 48 additions & 0 deletions source/crud/insert.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,54 @@ operation and an insert many operation:
insertOne() document id: BsonObjectId{value=...}
insertMany() document ids: {0=BsonObjectId{value=...}, 1=BsonObjectId{value=...}}

.. _java-insert-troubleshooting:

Troubleshooting
---------------

.. include:: /includes/crud/write-error.rst

Write Exception
~~~~~~~~~~~~~~~

The driver creates a write exception when it creates a ``WriteError`` object. The
driver throws a `MongoWriteException <{+core-api+}/MongoWriteException.html>`__
for any write errors that occur when performing single write operations.

A ``MongoWriteException`` object has an ``error`` field containing the
``WriteError`` object that caused it.

Example
```````

For example, the driver throws a ``MongoWriteException`` if you
attempt to insert a document into a collection that violates the collection's
schema validation rules. Suppose the collection has a rule where the value of
the ``quantity`` field must be an ``int`` type. If you attempt to insert a
document where the value of ``quantity`` is ``"three"``, the driver prints the
following error message:

.. code-block:: none
:copyable: false
:emphasize-lines: 1, 4-7

Exception in thread "main" com.mongodb.MongoWriteException: Document failed validation at
com.mongodb.internal.connection.ProtocolHelper.getWriteException(ProtocolHelper.java:228)
...
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"} } } at
com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)

In the preceding error message, the ``MongoWriteException`` object provides a
high-level description of the error. The ``WriteError`` object provides details
on the failed operation, such as the schema rules and the offending document. To
address this error, you must either revise the document to adhere to the schema
validation rules or bypass validation.

To learn more about schema validation, see :manual:`Schema Validation </core/schema-validation/>` in the Server Manual.

Additional Information
----------------------

Expand Down
31 changes: 31 additions & 0 deletions source/includes/crud/write-error.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.. _java_write_error:

This section explains write exceptions you might encounter when using the
{+driver-long+} to perform MongoDB write operations. Once you learn how to
understand the write exceptions that the driver raises, you can take appropriate
actions to either handle them or correct the error-causing code.

.. note::

This section addresses only write exception handling. If you encounter
any other issues with MongoDB or the driver, visit the following
resources:

- :ref:`java-connection-troubleshooting` for potential solutions to issues
you might encounter when connecting to a MongoDB deployment
- :ref:`java-issues-and-help` page for information about reporting bugs,
contributing to the driver, and finding more resources
- :community-forum:`MongoDB Community Forums </tag/java>` for questions,
discussions, or general technical support

Write Error
~~~~~~~~~~~

If the driver encounters an error while performing a write operation, it
creates an error object of the `WriteError <{+core-api+}/WriteError.html>`__ type.

The ``WriteError`` type contains the following fields:

- ``code``: the code associated with the error
- ``message``: a message explaining the error
- ``details``: an optional field containing details associated with the error
Loading