Bulk.find.updateOne()
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()
方法来执行批量写入操作。
Description描述
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.Bulk.find()
方法指定确定更新哪个文档的条件。TheBulk.find.updateOne()
method limits the update to a single document.Bulk.find.updateOne()
方法将更新限制为单个文档。To update multiple documents, see要更新多个文档,请参阅Bulk.find.update()
.Bulk.find.update()
。Bulk.find.updateOne()
accepts the following parameter:接受以下参数:Parameter参数Type类型Description描述update
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()
.Bulk.find.replaceOne()
。Update document更新文档Contains only update operator expressions.仅包含更新运算符表达式。Aggregation pipeline聚合管道
Starting in MongoDB 4.2在MongoDB 4.2中启动
Contains only the following aggregation stages:仅包含以下聚合阶段:$addFields
and its alias及其别名$set
$project
and its alias及其别名$unset
$replaceRoot
and its alias及其别名$replaceWith
.
For more information on the update modification parameter, see the有关更新修改参数的更多信息,请参阅db.collection.updateOne()
reference page.db.collection.updateOne()
参考页。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()
一起使用。To specify要指定arrayFilters
to update specific array elements, use withBulk.find.arrayFilters()
.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 also要批量替换文档,请参阅Bulk.find.replaceOne()
.Bulk.find.replaceOne()
。
Behavior行为
If the 如果<update>
document contains only update operator expressions, as in:<update>
文档仅包含更新运算符表达式,如中所示:
{
$set: { status: "D" },
$inc: { points: 2 }
}
Then, 然后,Bulk.find.updateOne()
updates only the corresponding fields, status
and points
, in the document.Bulk.find.updateOne()
只更新文档中相应的字段、status
和points
。
Example实例
The following example initializes a 以下示例初始化items集合的Bulk()
operations builder for the items
collection, and adds various updateOne()
operations to the list of operations.Bulk()
操作生成器,并将各种updateOne()
操作添加到操作列表中。
var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "D" } ).updateOne( { $set: { status: "I", points: "0" } } );
bulk.execute();
Update with Aggregation Pipeline使用聚合管道更新
Starting in version 4.2, update methods can accept an aggregation pipeline. For example, the following uses:从4.2版本开始,更新方法可以接受聚合管道。例如,以下使用:
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" } ).updateOne(
[
{ $set: { points: 0, lastModified: "$$NOW" } }
]
);
bulk.execute();