Docs Menu

Bulk.find.updateOne()

Tip

MongoDB は、一括書き込み操作を実行するための db.collection.bulkWrite() メソッドも提供します。

Bulk.find.updateOne(<update>)

Adds a single document update operation to a bulk operations list.

Use the Bulk.find() method to specify the condition that determines which document to update. The Bulk.find.updateOne() method limits the update to a single document. To update multiple documents, see Bulk.find.update().

Bulk.find.updateOne()は次のパラメータを受け入れます。

Parameter
タイプ
説明

ドキュメントまたはパイプライン

適用される変更内容。次のいずれかになります。

A replacement document

Contains only field and value pairs.

Bulk.find.replaceOne() も参照してください。

Update document

更新演算子式のみが含まれます。

Aggregation Pipeline

次の集計ステージのみが含まれます。

For more information on the update modification parameter, see the db.collection.updateOne() reference page.

The sum of the associated <query> document from the Bulk.find() and the update document must be less than or equal to the maximum BSON document size.

このコマンドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

注意

このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。

If the <update> document contains only update operator expressions, as in:

{
$set: { status: "D" },
$inc: { points: 2 }
}

Then, Bulk.find.updateOne() updates only the corresponding fields, status and points, in the document.

次の例では、 itemsコレクションでBulk()操作ビルダを初期化し、さまざまなupdateOne()操作を操作リストに追加します。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "D" } ).updateOne( { $set: { status: "I", points: "0" } } );
bulk.execute();

更新メソッドは集計パイプラインを受け入れることができます。 たとえば、次の例では次のものが使用されています。

  • $set ステージは、$set 更新演算子式と同様に動作します。

  • the aggregation variable NOW, which resolves to the current datetime and can provide similar behavior to a $currentDate update operator expression. To access aggregation variables, prefix the variable with double dollar signs $$ and enclose in quotes.

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P" } ).updateOne(
[
{ $set: { points: 0, lastModified: "$$NOW" } }
]
);
bulk.execute();

以下も参照してください。