How do we know if a bulk upsert fails partially

Good afternoon! After using bulk upsert now our application is 5 to 10 times faster. However, because one course of MongoDB University and other sources say that bulk operations may partially fail in rare instance, we really need to check if a bulk upsert fails or not. For the following code snippet using Spring Data MongoDB:

 BulkWriteResult bulkUpsert(List<Product> products) {
        BulkOperations bulkOperations = template.bulkOps(BulkOperations.BulkMode.UNORDERED, Product.class);
        products.forEach(product -> {
            Query query = new Query().addCriteria(Criteria.where("_id").is(product.id));
            bulkOperations.replaceOne(query, product, FindAndReplaceOptions.options().upsert());
        });

        return bulkOperations.execute();
    }

after each call to bulkUpsert() how can we know whether MongoDB has successfully executed the bulk operation? Do we always get an exception when MongoDB fails to upsert the documents partially?

Thanks a lot, in advance!

Daniel Li

The API seems to return

See the documentation for more information.

Thanks a lot, @steevej ! https://mongodb.prakticum-team.ru/docs/v6.2/reference/method/BulkWriteResult/ contains BulkWriteResult.writeErrors and BulkWriteResult.writeConcernError. If my understanding is correct, the BulkWriteResult instance returned by the MongoDB Java driver does not contain BulkWriteResult.writeErrors or BulkWriteResult.writeConcernError. Do you know how to get BulkWriteResult.writeErrors or BulkWriteResult.writeConcernError in Java? Thanks a lot, in advance!

From what I understand for writeErrors and writeConcernError you get exceptions:

https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/MongoBulkWriteException.html

Thanks a lot again, @steevej! This is also my guess. The MongoDB Java driver needs to provide a notification when a bulk upsert fails partially.