@@ -623,6 +623,63 @@ Even though the write operation inserting a document with a duplicate key result
623623in an error, the other operations are executed because the write operation is
624624unordered.
625625
626+ Troubleshooting
627+ ---------------
628+
629+ .. include:: /includes/crud/write-error.rst
630+
631+ Bulk Write Exception
632+ ~~~~~~~~~~~~~~~~~~~~
633+
634+ The driver creates write exceptions when a ``WriteError`` object is created. If an
635+ error is detected during a bulk write operation, a `MongoBulkWriteException
636+ <{+core-api+}/MongoBulkWriteException.html>`__ is thrown.
637+
638+ A ``MongoBulkWriteException`` contains a ``writeErrors`` field containing a list
639+ of one or more ``WriteError`` objects associated with the same bulk write
640+ operation.
641+
642+ Example
643+ '''''''
644+
645+ For example, the driver raises a ``MongoBulkWriteException`` if your bulk insert
646+ contains two documents into a collection that violate the collection's schema
647+ validation rules. Suppose the collection has a rule where the value of the
648+ ``quantity`` field must be an ``int`` type. If your bulk insert contains a
649+ document with a ``quantity`` of ``"three"`` and another with a ``quantity`` of
650+ ``"ten"`, the driver prints the following error message:
651+
652+ .. code-block:: none
653+ :copyable: false
654+ :emphasize-lines: 1-2, 6-9, 13-16
655+
656+ Exception in thread "main" com.mongodb.MongoBulkWriteException: Bulk write
657+ operation result had errors at
658+ com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:258)
659+ ... at
660+ BulkWriteMultipleValidationErrorsExample.main(BulkWriteMultipleValidationErrorsExample.java:30)
661+ Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
662+ message='Document failed validation', details={ operator: "$jsonSchema",
663+ schemaRules: { bsonType: "int", description: "must be an integer" },
664+ offendingDocument: {"name":"Apple","quantity":"three"} }} at
665+ com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
666+ at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
667+ ... 19 more
668+ Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
669+ message='Document failed validation', details={ operator: "$jsonSchema",
670+ schemaRules: { bsonType: "int", description: "must be an integer" },
671+ offendingDocument: {"name":"Banana","quantity":"ten"} }} at
672+ com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
673+ at
674+ com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
675+ ... 19 more
676+
677+ This exception message contains a list of the two ``WriteError`` objects. The
678+ description in the ``MongoBulkWriteException`` object is vague as
679+ documents associated with the same bulk operation could produce different error
680+ types. Refer to the individual ``WriteError`` objects' ``message`` fields to
681+ determine the cause of each error.
682+
626683Additional Information
627684----------------------
628685
0 commit comments