Tip
MongoDB also provides the MongoDB还提供了Mongo.bulkWrite() method for performing bulk write operations.Mongo.bulkWrite()方法来执行批量写入操作。
Description描述
Bulk.find.update(<update>)Adds a将multiupdate operation to a bulk operations list. The method updates specific fields in existing documents.multi更新操作添加到批量操作列表中。该方法更新现有文档中的特定字段。Use the使用Bulk.find()method to specify the condition that determines which documents to update.Bulk.find()方法指定确定要更新哪些文档的条件。TheBulk.find.update()method updates all matching documents. To specify a single document update, seeBulk.find.updateOne().Bulk.find.update()方法更新所有匹配的文档。要指定单个文档更新,请参阅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聚合管道Contains only the following aggregation stages:仅包含以下聚合阶段:$addFieldsand its alias及其别名$set$projectand its alias及其别名$unset$replaceRootand its alias及其别名$replaceWith
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 theBulk.find()and the update document must be less than or equal to the maximum BSON document size.Bulk.find()的关联<query>文档和更新文档的总和必须小于或等于BSON文档的最大大小。To specify an upsert: true for this operation, use with要为此操作指定一个Bulk.find.upsert().upsert: true,请与Bulk.find.upsert()一起使用。With使用Bulk.find.upsert(), if no documents match theBulk.find()query condition, the update operation inserts only a single document.Bulk.find.upsert(),如果没有文档符合Bulk.find()查询条件,则更新操作只插入一个文档。To specify要指定arrayFilters来更新特定的数组元素,请与arrayFiltersto update specific array elements, use withBulk.find.arrayFilters().Bulk.find.arrayFilters()一起使用。To specify the index to use for the associated要指定用于关联的Bulk.find(), seeBulk.find.hint().Bulk.find()的索引,请参阅Bulk.find.hint()。To replace a document wholesale, see要批量替换文档,请参阅Bulk.find.replaceOne().Bulk.find.replaceOne()。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
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使用聚合管道进行更新
Update methods can accept an aggregation pipeline. For example, the following uses:更新方法可以接受聚合管道。例如,以下使用:
the$setstage which can provide similar behavior to the$setupdate operator expression,$set阶段可以提供与$set更新运算符表达式类似的行为,the aggregation variable聚合变量NOW, which resolves to the current datetime and can provide similar behavior to a$currentDateupdate operator expression. To access aggregation variables, prefix the variable with double dollar signs$$and enclose in quotes.NOW解析为当前日期时间,并可以提供与$currentDate更新运算符表达式类似的行为。要访问聚合变量,请在变量前添加双美元符号$$并括在引号中。
var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P" } ).update(
[
{ $set: { points: 0, lastModified: "$$NOW" } }
]
);
bulk.execute();