Bulk.find.updateOne()
Tip
Starting in version 3.2, MongoDB also provides thedb.collection.bulkWrite()
method for performing bulkwrite operations.
Description
New in version 2.6.
Adds a single document update operation to a bulk operations list.
Use the Bulk.find()
method to specify the condition thatdetermines which document to update. TheBulk.find.updateOne()
method limits the update to a singledocument. To update multiple documents, seeBulk.find.update()
.
Bulk.find.updateOne()
accepts the following parameter:
ParameterTypeDescriptionupdate
documentThe modifications to apply.
The value can be either:
- A document that contains update operator expressions, or
- Starting in MongoDB 4.2, an aggregation pipeline. The pipeline canconsist of the following stages:
$addFields
and its alias$set
$project
and its alias$unset
$replaceRoot
and its alias$replaceWith
.For more information on the update modification parameter, see thedb.collection.updateOne
reference page.
The sum of the associated <query>
document from theBulk.find()
and the update document must beless 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 specify which array elementsto update, use with
Bulk.find.arrayFilters()
. - To specify the index to use for the associated
Bulk.find()
, seeBulk.find.hint()
. - To replace a document wholesale, see
Bulk.find.replaceOne()
.
Behavior
If the <update>
document contains only update operator expressions, as in:
- {
- $set: { status: "D" },
- $inc: { points: 2 }
- }
Then, Bulk.find.updateOne()
updates only the correspondingfields, status
and points
, in the document.
Example
The following example initializes a Bulk()
operations builderfor the items
collection, and adds variousupdateOne
operations to the list of operations.
- var bulk = db.items.initializeUnorderedBulkOp();
- bulk.find( { status: "D" } ).updateOne( { $set: { status: "I", points: "0" } } );
- bulk.execute();
See also