Bulk.find.update()
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 multi
update operation to a bulk operations list. Themethod updates specific fields in existing documents.
Use the Bulk.find()
method to specify the condition thatdetermines which documents to update. TheBulk.find.update()
method updates all matching documents.To specify a single document update, seeBulk.find.updateOne()
.
Bulk.find.update()
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.updateMany
reference page.
The sum of the associated <query>
document from theBulk.find()
and the update document must be less than orequal to the maximum BSON document size
.
- To specify an upsert: true for this operation,use with
Bulk.find.upsert()
. WithBulk.find.upsert()
, if no documents match theBulk.find()
query condition, the update operationinserts only a single document. - 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()
.
Example
The following example initializes a Bulk()
operations builderfor the items
collection, and adds various multi
updateoperations to the list of operations.
- var bulk = db.items.initializeUnorderedBulkOp();
- bulk.find( { status: "D" } ).update( { $set: { status: "I", points: "0" } } );
- bulk.find( { item: null } ).update( { $set: { item: "TBD" } } );
- bulk.execute();
See also