以下示例将插入一个新的科学家"Pierre Curie",通过将 "scientist" 字段的值设置为"Marie Curie",并使用 id "3" 删除记录:
// route: from("direct:bulkWrite").to("mongodb:myDb?database=science&collection=notableScientists&operation=bulkWrite");
List<WriteModel<DBObject>> bulkOperations = Arrays.asList(
new InsertOneModel<>(new BasicDBObject("scientist", "Pierre Curie")),
new UpdateOneModel<>(new BasicDBObject("_id", "5"),
new BasicDBObject("$set", new BasicDBObject("scientist", "Marie Curie"))),
new DeleteOneModel<>(new BasicDBObject("_id", "3")));
BulkWriteResult result = template.requestBody("direct:bulkWrite", bulkOperations, BulkWriteResult.class);
// route: from("direct:bulkWrite").to("mongodb:myDb?database=science&collection=notableScientists&operation=bulkWrite");
List<WriteModel<DBObject>> bulkOperations = Arrays.asList(
new InsertOneModel<>(new BasicDBObject("scientist", "Pierre Curie")),
new UpdateOneModel<>(new BasicDBObject("_id", "5"),
new BasicDBObject("$set", new BasicDBObject("scientist", "Marie Curie"))),
new DeleteOneModel<>(new BasicDBObject("_id", "3")));
BulkWriteResult result = template.requestBody("direct:bulkWrite", bulkOperations, BulkWriteResult.class);
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
默认情况下,操作会按顺序执行,并在不处理列表中剩余写入操作的情况下,在第一次写入错误中断。要指示 MongoDB 继续处理列表中剩余的写入操作,请将 CamelMongoDbBulkOrdered IN 消息标头设置为 false。未排序的操作会并行执行,这个行为无法保证。