38.7.4. 一括書き込み操作
38.7.4.1. bulkWrite リンクのコピーリンクがクリップボードにコピーされました!
実行順序を制御して書き込み操作を一括で実行します。挿入、更新、および削除操作のコマンドを含む IN メッセージ本文として List<WriteModel<Document>> が必要です。
次の例では、新しい科学者 "Pierre Curie" を挿入し、"scientist" フィールドの値を "Marie Curie" に設定して ID "5" のレコードを更新し、ID "3" のレコードを削除します。
// route: from("direct:bulkWrite").to("mongodb:myDb?database=science&collection=notableScientists&operation=bulkWrite");
List<WriteModel<Document>> bulkOperations = Arrays.asList(
new InsertOneModel<>(new Document("scientist", "Pierre Curie")),
new UpdateOneModel<>(new Document("_id", "5"),
new Document("$set", new Document("scientist", "Marie Curie"))),
new DeleteOneModel<>(new Document("_id", "3")));
BulkWriteResult result = template.requestBody("direct:bulkWrite", bulkOperations, BulkWriteResult.class);
デフォルトでは、操作は順番に実行され、最初の書き込みエラーで中断され、リスト内の残りの書き込み操作は処理されません。リスト内の残りの書き込み操作の処理を続行するように MongoDB に指示するには、CamelMongoDbBulkOrdered IN メッセージヘッダーを false に設定します。順序付けされていない操作は並行して実行され、この動作は保証されません。
| ヘッダーのキー | クイック定数 | 説明 (MongoDB API ドキュメントから抜粋) | 想定されるタイプ |
|---|---|---|---|
|
|
| 順序付きまたは順序なしの操作実行を実行します。デフォルトは true です。 | boolean/Boolean |