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
update 変更パラメーターの詳細については、
db.collection.updateOne()
のリファレンス ページを参照してください。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()
.特定の配列要素を更新するために
arrayFilters
を指定するには、Bulk.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.
例
The following example initializes a Bulk()
operations builder
for the items
collection, and adds various
updateOne()
operations to the list of operations.
var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { status: "D" } ).updateOne( { $set: { status: "I", points: "0" } } ); bulk.execute();
集約パイプラインによるアップデート
更新メソッドは集計パイプラインを受け入れることができます。 たとえば、次の例では次のものが使用されています。
集計変数
NOW
は現在の日時を解決し、$currentDate
更新演算子式と同様に動作します。集計変数にアクセスするには、変数の前に二重ドル記号$
を付け、引用符で囲みます。
var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { status: "P" } ).updateOne( [ { $set: { points: 0, lastModified: "$$NOW" } } ] ); bulk.execute();