翻译或纠错本页面

Bulk.find.updateOne()

Tip

Starting in version 3.2, MongoDB also provides the db.collection.bulkWrite() method for performing bulk write operations.

Description

Bulk.find.updateOne(<update>)

2.6 新版功能.

Adds a single document update operation to a bulk operations list. The operation can either replace an existing document or update specific fields in an existing document.

Use the Bulk.find() method to specify the condition that determines which document to update. The Bulk.find.updateOne() method limits the update or replacement to a single document. To update multiple documents, see Bulk.find.update().

Bulk.find.updateOne() accepts the following parameter:

Parameter Type Description
update document

An update document that updates specific fields or a replacement document that completely replaces the existing document.

An update document only contains update operator expressions. A replacement document contains only field and value pairs.

The sum of the associated <query> document from the Bulk.find() and the update/replacement document must be less than or equal to the maximum BSON document size.

To specify an upsert: true for this operation, see Bulk.find.upsert().

Behavior

Update Specific Fields

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.

Replace a Document

If the <update> document contains only field:value expressions, as in:

{
  item: "TBD",
  points: 0,
  inStock: true,
  status: "I"
}

Then, Bulk.find.updateOne() replaces the matching document with the <update> document with the exception of the _id field. The Bulk.find.updateOne() method does not replace the _id value.

Example

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.find( { item: null } ).updateOne(
   {
      item: "TBD",
      points: 0,
      inStock: true,
      status: "I"
   }
);
bulk.execute();