On this page本页内容
Starting in version 3.2, MongoDB also provides the 从3.2版开始,MongoDB还提供了用于执行批量写入操作的db.collection.bulkWrite()
method for performing bulk write operations.db.collection.bulkWrite()
方法。
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:接受以下参数:
update | document or pipeline |
|
Bulk.find.upsert()
. upsert:true
,请与Bulk.find.upsert()
一起使用。Bulk.find.upsert()
, if no documents match the Bulk.find()
query condition, the update operation inserts only a single document.Bulk.find.upsert()
,如果没有文档与Bulk.find()
查询条件匹配,则更新操作仅插入单个文档。arrayFilters
to update specific array elements, use with Bulk.find.arrayFilters()
.arrayFilters
来更新特定的数组元素,请与Bulk.find.arrayFilters()
一起使用。Bulk.find()
, see Bulk.find.hint()
.Bulk.find()
的索引,请参阅Bulk.find.hint()
。Bulk.find.replaceOne()
.Bulk.find.replaceOne()
。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();
Starting in version 4.2, update methods can accept an aggregation pipeline. 从4.2版开始,更新方法可以接受聚合管道。For example, the following uses:例如,以下使用:
$set
stage which can provide similar behavior to the $set
update operator expression,$set
阶段可以提供与$set
更新运算符表达式类似的行为,NOW
, which resolves to the current datetime and can provide similar behavior to a $currentDate
update operator expression. NOW
,它解析为当前日期时间,可以提供与$currentDate
更新运算符表达式类似的行为。$$
and enclose in quotes.$$
并用引号括起来。var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { status: "P" } ).update( [ { $set: { points: 0, lastModified: "$$NOW" } } ] ); bulk.execute();