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. TheBulk.find.updateOne()
method limits the update to a single document. To update multiple documents, seeBulk.find.update()
.Bulk.find.updateOne()
は次のパラメータを受け入れます。Parameterタイプ説明ドキュメントまたはパイプライン
適用される変更内容。次のいずれかになります。
A replacement document
Contains only field and value pairs.
Update document
更新演算子式のみが含まれます。
Aggregation Pipeline
次の集計ステージのみが含まれます。
$addFields
とそのエイリアス$set
$replaceRoot
とそのエイリアス$replaceWith
For more information on the update modification parameter, see the
db.collection.updateOne()
reference page.The sum of the associated
<query>
document from theBulk.find()
and the update document must be less than or equal to the maximum BSON document size.To specify an upsert: true for this operation, use with
Bulk.find.upsert()
.To specify
arrayFilters
to update specific array elements, use withBulk.find.arrayFilters()
.関連する
Bulk.find()
に使用するインデックスを指定するには、Bulk.find.hint()
を参照してください。
To replace a document wholesale, see also
Bulk.find.replaceOne()
.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
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();
集約パイプラインによるアップデート
更新メソッドは集計パイプラインを受け入れることができます。 たとえば、次の例では次のものが使用されています。
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();
以下も参照してください。