Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Java Sync Driver
/

アップデート ビルダ

このガイドでは、Java ドライバーで ビルダ を使用して アップデート を指定する方法を学習できます。

Updatesビルダは、次のタスクを簡素化するためのヘルパー メソッドを提供します。

  • フィールド更新: 特定のフィールドの値を更新または削除

  • 配列の更新: 配列値のあるフィールドの値の更新

  • 複数の更新演算子の組み合わせ: 複数のフィールドを設定や変更するなど、一度に複数の更新を実行

ドキュメントの更新を期待するいくつかのメソッドは次のとおりです。

  • updateOne()

  • updateMany()

  • bulkWrite()

Updatesクラスは、すべての MongoDB 更新演算子の静的ファクトリー メソッドを提供します。 各メソッドはBSON型のインスタンスを返します。このインスタンスは、アップデート引数を必要とする任意のメソッドに渡すことができます。

Tip

簡潔にするために、アップデート クラスのメソッドを静的にインポートすることを選択できます。

import static com.mongodb.client.model.Updates.*;

次の例では、この静的インポートを前提としています。

このガイドの例では、次のドキュメントを使用します。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

set() メソッドを使用して、 更新操作でフィールドの値を割り当てます。

次の例では、 qtyフィールドの値を 11 に設定します。

Bson filter = eq("_id", 1);
Bson update = set("qty", 11);
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 11,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

次の例では、元のドキュメントに 2 つの新しいフィールドを追加します。

Bson filter = eq("_id", 1);
Bson update = combine(set("width", 6.5), set("height", 10));
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" },
"width": 6.5,
"height": 10,
}

更新操作でフィールドの値を削除するには、 unset() メソッドを使用します。

次の例では、 qtyフィールドを削除しています。

Bson filter = eq("_id", 1);
Bson update = unset("qty");
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

setOnInsert() メソッドを使用して、ドキュメントの挿入でのアップデート操作でフィールドの値を割り当てます。

次の例では、アップサートによってドキュメントが挿入された場合に、 qtyフィールドの値を 5 に設定します。

Bson filter = eq("_id", 1);
Bson update = setOnInsert("qty", 7);
collection.updateOne(filter, update, new UpdateOptions().upsert(true));

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

注意

ドキュメントが挿入されていない場合は、変更は行われません。

inc() メソッドを使用して、 更新操作で数値フィールドの値を増加させます。

次の例では、 qtyフィールドの値を「3」ずつ増加させます。

Bson filter = eq("_id", 1);
Bson update = inc("qty", 3);
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 8,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

更新操作で数値フィールドの値を乗算するには、mul() メソッドを使用します。

次の例えでは、 qtyフィールドの値に「2」を掛けます。

Bson filter = eq("_id", 1);
Bson update = mul("qty", 2);
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 10,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

rename() メソッドを使用して、 更新操作内のフィールドの値の名前を変更します。

次の例では、 qtyフィールドの名前を「q」に変更します。

Bson filter = eq("_id", 1);
Bson update = rename("qty", "quantity");
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" },
"quantity": 5
}

min() メソッドを使用して、 更新操作で指定された 2 つのうち小さい方の数を持つフィールドの値を更新します。

Bson filter = eq("_id", 1);
Bson update = min("qty", 2);
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 2,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

max() メソッドを使用して、 更新操作で指定された 2 つののうち大きい方の数を持つフィールドの値を更新します。

次の例では、 qtyフィールドの値を、現在の値の最大値と「8」に設定します。

Bson filter = eq("_id", 1);
Bson update = max("qty", 8);
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 8,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

currentDate() メソッドを使用して、アップデート操作内のフィールドの値をBSON日付として現在の日付に割り当てます。

次の例では、 lastModifiedフィールドの値を BSON 日付として現在の日付に設定します。

Bson filter = eq("_id", 1);
Bson update = currentDate("lastModified");
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-22T21:01:20.027Z" }
}

currentTimestamp() メソッドを使用して、アップデート操作内のフィールドの値をタイムスタンプとして現在の日付に割り当てます。

次の例では、 lastModifiedフィールドの値を BSON タイムスタンプとして現在の日付に設定します。

Bson filter = eq("_id", 1);
Bson update = currentTimestamp("lastModified");
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$timestamp": { "t": 1616446880, "i": 5 } }
}

bitizeOr() bitWithAnd() bitwiftXor() メソッドを使用して、 更新操作でフィールドの整数値のビット単位の更新を実行します。

次の例では、数値「10」とqtyフィールドの整数値の間でビット単位のANDを実行します。

Bson filter = eq("_id", 1);
Bson update = bitwiseOr("qty", 10);
collection.updateOne(filter, update);

ビット単位の 演算の結果は 15 になります。

0101
1010
----
1111

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 15,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

値がまだ 更新操作に存在しない場合に、配列に値を追加するには、 addToSet() メソッドを使用します。

次の例では、 vendorフィールドの配列値に値「C」を追加します。

Bson filter = eq("_id", 1);
Bson update = addToSet("vendor", "C");
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M", "C" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

更新操作において、配列の最初の要素を削除するにはpopFirst() メソッドを使用し、配列の最後の要素を削除するには opLast() メソッドを使用します。

次の例では、 vendorフィールドの配列値の最初の要素をポップします。

Bson filter = eq("_id", 1);
Bson update = popFirst("vendor");
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

PullAll() メソッドを使用して、 更新操作で既存の配列から 値のすべてのインスタンスを削除します。

次の例では、 vendor配列からベンダー "A" と "M" を削除します。

Bson filter = eq("_id", 1);
Bson update = pullAll("vendor", Arrays.asList("A", "M"));
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "D" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Pull() メソッドを使用して、 更新操作で既存の配列から値のすべてのインスタンスを削除します。

次の例では、 vendor配列から値「D」を削除します。

Bson filter = eq("_id", 1);
Bson update = pull("vendor", "D");
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

push() メソッドを使用して、 更新操作において配列に値を追加します。

次の例では「C」をvendor配列にプッシュします。

Bson filter = eq("_id", 1);
Bson update = push("vendor", "C");
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M", "C" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

アプリケーションは、前のセクションで説明された更新演算子を 2 つ以上組み合わせて、単一のドキュメントの複数のフィールドを更新できます。

次の例では、 qtyフィールドの値を「6」ずつ増加させ、 colorフィールドの値を「pull」に設定して、 vendorフィールドに「R」をプッシュします。

Bson filter = eq("_id", 1);
Bson update = combine(set("color", "purple"), inc("qty", 6), push("vendor", "R"));
collection.updateOne(filter, update);

上記の例では、元のドキュメントを次の状態にアップデートします。

{
"_id": 1,
"color": "purple",
"qty": 11,
"vendor": [ "A", "D", "M", "R" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

戻る

Sort

項目一覧