@@ -623,6 +623,54 @@ Even though the write operation inserting a document with a duplicate key result
623
623
in an error, the other operations are executed because the write operation is
624
624
unordered.
625
625
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
+
626
674
Additional Information
627
675
----------------------
628
676
@@ -637,6 +685,7 @@ MongoCollection
637
685
638
686
- `bulkWrite() <{+driver-api+}/MongoCollection.html#bulkWrite(com.mongodb.client.ClientSession,java.util.List)>`__
639
687
- `BulkWriteOptions <{+core-api+}/client/model/BulkWriteOptions.html>`__
688
+ - `WriteError <{+core-api+}/WriteError.html>`__
640
689
- `MongoBulkWriteException <{+core-api+}/MongoBulkWriteException.html>`__
641
690
642
691
MongoClient
@@ -651,3 +700,4 @@ Server Manual Entries
651
700
652
701
- :manual:`MongoCollection.bulkWrite() </reference/method/db.collection.bulkWrite/>`
653
702
- :manual:`MongoClient.bulkWrite() </reference/command/bulkWrite/>`
703
+ - :manual:`Schema Validation </core/schema-validation/>`
0 commit comments