Docs Menu

Bulk.find.updateOne()

On this page

  • Description
  • Behavior
  • Example
Tip
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() accepts the following parameter:

Parameter
Type
Description
document or pipeline

The modifications to apply. Can be one of the following:

A replacement document

Contains only field and value pairs.

See also Bulk.find.replaceOne().

Update document

Aggregation pipeline

Starting in MongoDB 4.2

Contains only the following aggregation stages:

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.

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();

Starting in version 4.2, update methods can accept an aggregation pipeline. For example, the following uses:

  • the $set stage which can provide similar behavior to the $set update operator expression,
  • 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();
Tip
See also:
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.