Skip to content

[backport v5.5] #710 Java Write Exception #712

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 26 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f289c53
DOCSP-30350 Java Write Error Page
mballard-mdb Jun 11, 2025
6bd5d2b
DOCSP-30350 add to toc tree and fix links
mballard-mdb Jun 11, 2025
4fef535
DOCSP-30350 toc tree update and editing
mballard-mdb Jun 11, 2025
ed39387
DOCSP-30350 code blocks and update write/bulkwrite sections
mballard-mdb Jun 11, 2025
f88722b
DOCSP-30350 Updating grammar and Write/bulk Write error messages
mballard-mdb Jun 11, 2025
e506c1b
DOCSP30350 fix code example format
mballard-mdb Jun 11, 2025
b7a0825
DOCSP-30350 code examples and additional getCategory information
mballard-mdb Jun 11, 2025
7a830c3
DOCSP-30350 fixing up minor issues with page, final check
mballard-mdb Jun 11, 2025
a288c4c
DOCSP30350 add exceptions not just errors to doc
mballard-mdb Jun 11, 2025
4ad76f4
DOCSP-30350 fixing formatting
mballard-mdb Jun 11, 2025
aef4ee5
DOCSP-30350 Write exceptions, reorg
mballard-mdb Jun 11, 2025
71889aa
DOCSP-30350 reorg again and edit writing on some descriptions
mballard-mdb Jun 11, 2025
ad892b5
DOCSP30350- add quotes on strings
mballard-mdb Jun 11, 2025
2bcc344
DOCSP-30350 toc tree naming update
mballard-mdb Jun 11, 2025
7eb677e
DOCSP-30350 add link to schema validation page
mballard-mdb Jun 11, 2025
dc1efbc
DOCSP-30350 schema validation link fix
mballard-mdb Jun 11, 2025
a603ff9
DOCSP-30350 put troubleshooting into main pages, not separate
mballard-mdb Jun 12, 2025
91d46f3
DOCSP-30350 Adding descriptions and editing
mballard-mdb Jun 12, 2025
fab3104
DOCSP-30350 fixing example headings and final edits
mballard-mdb Jun 12, 2025
f0fc822
DOCSP-30350 making vocab consistent, active voice
mballard-mdb Jun 12, 2025
5b539a6
DOCSP-30350 fix typo
mballard-mdb Jun 12, 2025
dc22ad5
DOCSP-30350 adding suggestions and API doc links
mballard-mdb Jun 13, 2025
00723b1
DOCSP-30350 small tweaks to punctuation and wording
mballard-mdb Jun 13, 2025
2587bb4
DOCSP-30350 final edits
mballard-mdb Jun 13, 2025
b763e2a
DOCSP-30350 add schema val link to bulk
mballard-mdb Jun 13, 2025
dab9444
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
50 changes: 50 additions & 0 deletions source/crud/bulk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,54 @@ 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
---------------

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

If the driver encounters an error 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.

Consider a collection that has a schema validation rule where the value of the
``quantity`` field must be an ``int`` type. In the following example, the driver
throws a ``MongoBulkWriteException`` when you attempt to insert a document with
a ``quantity`` field value of ``"three"`` and another with a ``quantity`` field
value of ``"ten"``.

.. 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

To learn more about schema validation, see Schema Validation in the Server
Manual Entries section.

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

Expand All @@ -637,6 +685,7 @@ MongoCollection

- `bulkWrite() <{+driver-api+}/MongoCollection.html#bulkWrite(com.mongodb.client.ClientSession,java.util.List)>`__
- `BulkWriteOptions <{+core-api+}/client/model/BulkWriteOptions.html>`__
- `WriteError <{+core-api+}/WriteError.html>`__
- `MongoBulkWriteException <{+core-api+}/MongoBulkWriteException.html>`__

MongoClient
Expand All @@ -651,3 +700,4 @@ Server Manual Entries

- :manual:`MongoCollection.bulkWrite() </reference/method/db.collection.bulkWrite/>`
- :manual:`MongoClient.bulkWrite() </reference/command/bulkWrite/>`
- :manual:`Schema Validation </core/schema-validation/>`
42 changes: 41 additions & 1 deletion source/crud/insert.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Insert Operations
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:depth: 2
:class: singlecol

Overview
Expand Down Expand Up @@ -189,6 +189,40 @@ operation and an insert many operation:
insertOne() document id: BsonObjectId{value=...}
insertMany() document ids: {0=BsonObjectId{value=...}, 1=BsonObjectId{value=...}}

.. _java-insert-troubleshooting:

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

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

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.

Consider a collection that has a schema validation rule where the value of the
``quantity`` field must be an ``int`` type. In the following example, the driver
throws a ``MongoWriteException`` if you attempt to insert a document where the
value of ``quantity`` is ``"three"``.

.. 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)

To learn more about schema validation, see Schema Validation in the Server
Manual Entries section.

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

Expand All @@ -202,8 +236,14 @@ For more information about the methods and classes used to insert documents, see
- `insertMany() <{+driver-api+}/MongoCollection.html#insertMany(java.util.List)>`__
- `InsertManyResult <{+core-api+}/client/result/InsertManyResult.html>`__

For more information about the error types discussed in the Troubleshooting section, see the following API documentation:

- `WriteError <{+core-api+}/WriteError.html>`__
- `MongoWriteException <{+core-api+}/MongoWriteException.html>`__

Server Manual Entries
~~~~~~~~~~~~~~~~~~~~~

- :manual:`db.collection.insertOne() </reference/method/db.collection.insertOne/>`
- :manual:`db.collection.insertMany() </reference/method/db.collection.insertMany/>`
- :manual:`Schema Validation </core/schema-validation/>`
Loading