Skip to content

Commit 6acddba

Browse files
authored
Merge pull request #710 from mballard-mdb/DOCSP-30350-java-write-exception-ts
DOCSP-30350 Java Write Exception Troubleshooting
2 parents 6cb020e + 4abbd49 commit 6acddba

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

source/crud/bulk.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,54 @@ Even though the write operation inserting a document with a duplicate key result
623623
in an error, the other operations are executed because the write operation is
624624
unordered.
625625

626+
.. _java-bulk-insert-troubleshooting:
627+
628+
Troubleshooting
629+
---------------
630+
631+
Bulk Write Exception
632+
~~~~~~~~~~~~~~~~~~~~
633+
634+
If the driver encounters an error during a bulk write operation, the driver
635+
throws a `MongoBulkWriteException.
636+
<{+core-api+}/MongoBulkWriteException.html>`__ A ``MongoBulkWriteException``
637+
contains a ``writeErrors`` field consisting of a list of one or more
638+
``WriteError`` objects associated with the same bulk write operation.
639+
640+
Consider a collection that has a schema validation rule where the value of the
641+
``quantity`` field must be an ``int`` type. In the following example, the driver
642+
throws a ``MongoBulkWriteException`` when you attempt to insert a document with
643+
a ``quantity`` field value of ``"three"`` and another with a ``quantity`` field
644+
value of ``"ten"``.
645+
646+
.. code-block:: none
647+
:copyable: false
648+
:emphasize-lines: 1-2, 6-9, 13-16
649+
650+
Exception in thread "main" com.mongodb.MongoBulkWriteException: Bulk write
651+
operation result had errors at
652+
com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:258)
653+
... at
654+
BulkWriteMultipleValidationErrorsExample.main(BulkWriteMultipleValidationErrorsExample.java:30)
655+
Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
656+
message='Document failed validation', details={ operator: "$jsonSchema",
657+
schemaRules: { bsonType: "int", description: "must be an integer" },
658+
offendingDocument: {"name":"Apple","quantity":"three"} }} at
659+
com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
660+
at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
661+
... 19 more
662+
Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
663+
message='Document failed validation', details={ operator: "$jsonSchema",
664+
schemaRules: { bsonType: "int", description: "must be an integer" },
665+
offendingDocument: {"name":"Banana","quantity":"ten"} }} at
666+
com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
667+
at
668+
com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
669+
... 19 more
670+
671+
To learn more about schema validation, see Schema Validation in the Server
672+
Manual Entries section.
673+
626674
Additional Information
627675
----------------------
628676

@@ -637,6 +685,7 @@ MongoCollection
637685

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

642691
MongoClient
@@ -651,3 +700,4 @@ Server Manual Entries
651700

652701
- :manual:`MongoCollection.bulkWrite() </reference/method/db.collection.bulkWrite/>`
653702
- :manual:`MongoClient.bulkWrite() </reference/command/bulkWrite/>`
703+
- :manual:`Schema Validation </core/schema-validation/>`

source/crud/insert.txt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Insert Operations
1515
.. contents:: On this page
1616
:local:
1717
:backlinks: none
18-
:depth: 1
18+
:depth: 2
1919
:class: singlecol
2020

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

192+
.. _java-insert-troubleshooting:
193+
194+
Troubleshooting
195+
---------------
196+
197+
Write Exception
198+
~~~~~~~~~~~~~~~
199+
200+
The driver throws a `MongoWriteException
201+
<{+core-api+}/MongoWriteException.html>`__ for any write errors that occur when
202+
performing single write operations. A ``MongoWriteException`` object has an
203+
``error`` field containing the ``WriteError`` object that caused it.
204+
205+
Consider a collection that has a schema validation rule where the value of the
206+
``quantity`` field must be an ``int`` type. In the following example, the driver
207+
throws a ``MongoWriteException`` if you attempt to insert a document where the
208+
value of ``quantity`` is ``"three"``.
209+
210+
.. code-block:: none
211+
:copyable: false
212+
:emphasize-lines: 1, 4-7
213+
214+
Exception in thread "main" com.mongodb.MongoWriteException: Document failed validation at
215+
com.mongodb.internal.connection.ProtocolHelper.getWriteException(ProtocolHelper.java:228)
216+
...
217+
Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
218+
message='Document failed validation', details={ operator: "$jsonSchema",
219+
schemaRules: { bsonType: "int", description: "must be an integer" },
220+
offendingDocument: {"name":"Apple","quantity":"three"} } } at
221+
com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
222+
223+
To learn more about schema validation, see Schema Validation in the Server
224+
Manual Entries section.
225+
192226
Additional Information
193227
----------------------
194228

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

239+
For more information about the error types discussed in the Troubleshooting section, see the following API documentation:
240+
241+
- `WriteError <{+core-api+}/WriteError.html>`__
242+
- `MongoWriteException <{+core-api+}/MongoWriteException.html>`__
243+
205244
Server Manual Entries
206245
~~~~~~~~~~~~~~~~~~~~~
207246

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

0 commit comments

Comments
 (0)