我正在使用 BulkWriteWithOptions 在数据库中插入多个文档。我想要插入的文档,以便我知道哪一个文档被插入、失败或重复。 以下是我正在使用的代码
mongoClient.bulkWriteWithOptions(collection, operations, options, repoAsyncResult -> {
if (repoAsyncResult.failed()) {
LOGGER.error("Bulk insertion failed : {}", repoAsyncResult.cause().getMessage());
if(repoAsyncResult.cause() instanceof MongoBulkWriteException ){
MongoBulkWriteException exception = (MongoBulkWriteException)repoAsyncResult.cause() ;
exception.getWriteErrors().forEach(error -> {
LOGGER.error("Insert Error : " + error.getMessage());
});
}
repoFuture.fail(repoAsyncResult.cause());
} else {
LOGGER.info("Bulk insertion successful : {}", repoAsyncResult.result().toJson());
repoFuture.complete(repoAsyncResult.result().toJson());
}
});
有什么办法可以得到插入的文档结果吗?
最佳答案
不可以,您只能从repoAsyncResult.result().getUpserts()
获取更新插入文档的ID (List<JsonObject>
,其 .getAsString("_id")
将返回更新插入的 ID。)
关于java - 使用 vertx 在 mongoDB 中批量写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49406233/