Bulk.find.update()

On this page本页内容

Tip提示

Starting in version 3.2, MongoDB also provides the db.collection.bulkWrite() method for performing bulk write operations.从3.2版开始,MongoDB还提供了用于执行批量写入操作的db.collection.bulkWrite()方法。

Description描述

Bulk.find.update(<update>)

Adds a multi update operation to a bulk operations list. multi更新操作添加到批量操作列表。The method updates specific fields in existing documents.该方法更新现有文档中的特定字段。

Use the Bulk.find() method to specify the condition that determines which documents to update. 使用Bulk.find()方法指定确定要更新哪些文档的条件。The Bulk.find.update() method updates all matching documents. Bulk.find.update()方法更新所有匹配的文档。To specify a single document update, see Bulk.find.updateOne().要指定单个文档更新,请参阅Bulk.find.updateOne()

Bulk.find.update() accepts the following parameter:接受以下参数:

Parameter参数Type类型Description描述
updatedocument or pipeline

The modifications to apply. 要应用的修改。Can be one of the following:可以是以下之一:

Update document更新文档Contains only update operator expressions.仅包含更新运算符表达式

Aggregation pipeline聚合管道

Starting in MongoDB 4.2从MongoDB 4.2开始

Contains only the following aggregation stages:仅包含以下聚合阶段:

For more information on the update modification parameter, see the db.collection.updateMany() reference page.有关更新修改参数的更多信息,请参阅db.collection.updateMany()参考页面。

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.Bulk.find()中关联的<query>文档和更新文档的总和必须小于或等于最大BSON文档大小

Example示例

The following example initializes a Bulk() operations builder for the items collection, and adds various multi update operations to the list of operations.下面的示例初始化items集合的Bulk()操作生成器,并将各种multi更新操作添加到操作列表中。

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

Update with Aggregation Pipeline使用聚合管道更新

Starting in version 4.2, update methods can accept an aggregation pipeline. 从4.2版开始,更新方法可以接受聚合管道。For example, the following uses:例如,以下使用:

  • the $set stage which can provide similar behavior to the $set update operator expression,$set阶段可以提供与$set更新运算符表达式类似的行为,
  • the aggregation variable NOW, which resolves to the current datetime and can provide similar behavior to a $currentDate update operator expression. 聚合变量NOW,它解析为当前日期时间,可以提供与$currentDate更新运算符表达式类似的行为。To access aggregation variables, prefix the variable with double dollar signs $$ and enclose in quotes.要访问聚合变量,请在变量前面加上双美元符号$$并用引号括起来。
var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P" } ).update(
   [
      { $set: { points: 0, lastModified: "$$NOW" } }
   ]
);
bulk.execute();
←  Bulk.find.updateOne()Bulk.find.upsert() →