ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs 菜单

findAndModify

findAndModify

The findAndModify command updates and returns a single document. By default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option.

提示

mongosh 中,该命令也可通过 db.collection.findAndModify() 辅助方法运行。

辅助方法对 mongosh 用户来说很方便,但它们返回的信息级别可能与数据库命令不同。如果不追求方便或需要额外的返回字段,请使用数据库命令。

此命令可用于以下环境中托管的部署:

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

5.0 版本中的更改

该命令具有以下语法:

db.runCommand(
{
findAndModify: <collection-name>,
query: <document>,
sort: <document>,
remove: <boolean>,
update: <document or aggregation pipeline>,
new: <boolean>,
fields: <document>,
upsert: <boolean>,
bypassDocumentValidation: <boolean>,
writeConcern: <document>,
maxTimeMS: <integer>,
collation: <document>,
arrayFilters: <array>,
hint: <document|string>,
comment: <any>,
let: <document> // Added in MongoDB 5.0
}
)

该命令接受以下字段:

字段
类型
说明

query

文档

Optional. The selection criteria for the modification. The query field employs the same query selectors as used in the db.collection.find() method. Although the query may match multiple documents, findAndModify will only select one document to update.

如果未指定,则默认为空文档。

If the query argument is not a document, the operation errors.

sort

文档

Optional. Determines which document the operation updates if the query selects multiple documents. findAndModify updates the first document in the sort order specified by this argument.

如果 sort 参数不是文档,则操作错误。

MongoDB 不按特定顺序将文档存储在集合中。对包含重复值的字段进行排序时,可能会以任何顺序返回包含这些值的文档。

如果需要一致的排序顺序,请在排序中至少纳入一个包含唯一值的字段。最简单方法是在排序查询中纳入 _id 字段。

有关更多信息,请参阅排序一致性

remove

布尔

Must specify either the remove or the update field. Removes the document specified in the query field. Set this to true to remove the selected document . The default is false.

update

文档或数组

Must specify either the remove or the update field. Performs an update of the selected document.

new

布尔

Optional. When true, returns the updated document rather than the original. The default is false.

fields

文档

Optional. A subset of fields to return. The fields document specifies an inclusion of a field with 1, as in: fields: { <field1>: 1, <field2>: 1, ... }. See 投射.

If the fields argument is not a document, the operation errors.

upsert

布尔

Optional. Used in conjunction with the update field.

When true, findAndModify either:

  • 如果没有文档与 query 匹配,则创建一个新文档。有关详细信息,请参阅更新或插入行为

  • 更新与 query 匹配的单份文档。

要避免多次更新或插入,请确保 query字段具有唯一索引。有关示例,请参阅使用唯一索引进行更新或插入。

默认值为 false;即,在未找到匹配项时插入新文档。

bypassDocumentValidation

布尔

可选。启用 findAndModify 可在此操作期间绕过模式验证。这样便可更新不符合验证要求的文档。

writeConcern

文档

可选。表达写关注的文档。省略以使用默认写关注。

如果是在事务中运行,则请勿显式设置此操作的写关注。要将写关注与事务一起使用,请参阅事务和写关注。

maxTimeMS

non-negative integer

可选。

指定时间限制(以毫秒为单位)。如果您未指定 maxTimeMS 值,操作将不会超时。如果值为 0 ,则显式指定默认无限制行为。

MongoDB 使用与 db.killOp() 相同的机制终止超过分配的时间限制的操作。MongoDB 仅在指定的中断点之一中终止操作。

findAndModify

字符串

The collection against which to run the command.

collation

文档

可选。

指定用于操作的排序规则

排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号规则。

排序规则选项的语法如下:

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}

指定排序规则时,locale 字段为必填字段;所有其他排序规则字段均为可选字段。有关字段的说明,请参阅排序规则文档

如果未指定排序规则,但集合具有默认排序规则(请参阅 db.createCollection()),则操作将使用为集合指定的排序规则。

如果没有为收集或操作指定排序规则,MongoDB 将使用先前版本中使用的简单二进制比较来进行字符串比较。

您不能为一个操作指定多个排序规则。例如,您不能为每个字段指定不同的排序规则,或者如果执行带排序的查找,则不能使用一种排序规则进行查找而另一种排序规则进行排序。

arrayFilters

阵列

选修的。大量过滤器文档,用于确定针对大量字段的更新操作要修改哪些大量元素。

在更新文档中,使用 $[<identifier>] 筛选后的位置运算符来定义标识符,然后在数组筛选文档中引用该标识符。如果更新文档中未包含某一标识符,则无法获得该标识符的数组筛选文档。

<identifier> 必须以小写字母开头,且只能包含字母数字字符。

您可以在更新文档中多次包含相同的标识符;但对于更新文档中的每个不同标识符 ($[identifier]),您必须准确指定一个对应的数组筛选器文档。也就是说,不能为同一个标识符指定多个数组筛选器文档。例如,如果更新语句包含标识符 x(可能多次),则不能为 arrayFilters 指定以下内容,其中包括 x 的 2 个单独的筛选器文档:

// INVALID
[
{ "x.a": { $gt: 85 } },
{ "x.b": { $gt: 80 } }
]

但是,您可以在单个过滤器文档中的同一标识符上指定复合条件,例如以下示例:

// Example 1
[
{ $or: [{"x.a": {$gt: 85}}, {"x.b": {$gt: 80}}] }
]
// Example 2
[
{ $and: [{"x.a": {$gt: 85}}, {"x.b": {$gt: 80}}] }
]
// Example 3
[
{ "x.a": { $gt: 85 }, "x.b": { $gt: 80 } }
]

有关示例,请参阅使用 arrayFilters进行数组更新操作。

arrayFilters不适用于使用聚合管道的更新。

hint

文档或字符串

Optional. A document or string that specifies the 索引(index) to use to support the query.

该选项可以采用索引规范文档或索引名称字符串。

如果指定不存在的索引,则操作出错。

For an example, see Specify hint for findAndModify Operations.

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

文档

可选。

指定包含变量列表的文档。这样可以将变量与查询文本分开,从而提高命令的可读性。

文档语法为:

{
<variable_name_1>: <expression_1>,
...,
<variable_name_n>: <expression_n>
}

变量设置为表达式返回的值,并且之后不能再进行更改。

要访问命令中的变量值,请使用双美元符号前缀 ($) 以及 $<variable_name> 形式的变量名称。例如:$targetTotal

要使用变量筛选结果,您必须在 $expr 操作符中访问该变量。

有关使用let 和变量的完整示例,请参阅在 let中使用变量。

版本 5.0 中的新增功能

The findAndModify command returns a document with the following fields:

字段
类型
说明

value

文档

Contains the command's returned value. See value for details.

lastErrorObject

文档

Contains information about updated documents. See lastErrorObject for details.

ok

数字

Contains the command's execution status. 1 on success, or 0 if an error occurred.

The lastErrorObject embedded document contains the following fields:

字段
类型
说明

n

整型

Contains the number of documents that matched the update predicate or the number of documents that the command inserted or deleted.

updatedExisting

布尔

Contains true if an update operation:

  • Updated an existing document.

  • Found the document, but it was already in the desired destination state so no update actually occurred.

upserted

文档

Contains the ObjectId of the inserted document if an update operation with upsert: true resulted in a new document.

For remove operations, value contains the removed document if the query matches a document. If the query does not match a document to remove, value contains null.

For update operations, the value embedded document contains the following:

  • If the new parameter is not set or is false:

    • the pre-modification document if the query matches a document;

    • otherwise, null.

  • If new is true:

    • the updated document if the query returns a match;

    • the inserted document if upsert: true and no document matches the query;

    • otherwise, null.

除非有唯一索引来防止重复,否则 upsert 可能会创建重复的文档。

以某一情况为例,其中不存在名为 Andy 的文档,且多个客户端大致会在同一时间发出以下命令:

db.runCommand(
{
findAndModify: "people",
query: { name: "Andy" },
update: { $inc: { score: 1 } },
upsert: true
}
)

如果所有 findAndModify 操作在任何客户端成功插入数据之前完成了查询阶段, name 字段上没有唯一索引,则每个 findAndModify 操作都可能导致插入,从而使用 name: Andy 创建多个文档。

name 字段上的唯一索引可确保仅创建一个文档。通过此唯一索引,多个 findAndModify 操作此时便有如下行为:

  • 只需执行一次 findAndModify 操作就能成功插入一个新文档。

  • 其他 findAndModify 操作要么更新新插入的文档,要么由于唯一键冲突而失败。

    为了让其他 findAndModify 操作更新新插入的文档,必须满足以下所有条件:

    • 目标collection具有会导致重复键错误的唯一索引。

    • 更新操作不是updateManymultifalse

    • 更新匹配条件为:

      • 单个相等谓词。例如 { "fieldA" : "valueA" }

      • 等式谓词的逻辑 AND。例如 { "fieldA" : "valueA", "fieldB" : "valueB" }

    • 相等谓词中的字段与唯一索引键模式中的字段匹配。

    • 更新操作不会修改唯一索引键模式中的任何字段。

下表显示了upsert操作的示例,当发生键冲突时,这些操作要么导致更新,要么失败。

唯一索引键模式
更新操作
结果
{ name : 1 }
db.people.updateOne(
{ name: "Andy" },
{ $inc: { score: 1 } },
{ upsert: true }
)

匹配文档的score字段递增 1。

{ name : 1 }
db.people.updateOne(
{ name: { $ne: "Joe" } },
{ $set: { name: "Andy" } },
{ upsert: true }
)

操作失败是因为它修改了唯一索引键模式 ( name ) 中的字段。

{ name : 1 }
db.people.updateOne(
{ name: "Andy", email: "andy@xyz.com" },
{ $set: { active: false } },
{ upsert: true }
)

操作失败,因为等值谓词字段( nameemail )与索引键字段( name )不匹配。

要在分片集合上使用 findAndModify

  • If you only target one shard, you can use a partial shard key in the query field or,

  • You can provide an equality condition on a full shard key in the query field.

  • 从版本 7.1 开始,您不需要在查询规范中提供分片键_id 字段。

分片集合中的文档可能缺少分片键字段。要定位缺失分片键的文档,可将 null 等值匹配与其他过滤条件(例如针对 _id 字段)结合使用。例如:

{ _id: <value>, <shardkeyfield>: null } // _id of the document missing shard key

您可以更新文档的分片键值,除非分片键字段是不可变的 _id 字段。

警告

分片集合中的文档可能缺少分片键字段。采取预防措施,避免在更改文档的分片键值时意外删除分片键。

To update the existing shard key value with findAndModify:

  • 必须mongos 上运行。请勿直接对分片发出此操作。

  • 必须事务中运行,或者以可重试写入的形式运行。

  • 必须在完整分片键上包含相等筛选器。

Documents in a sharded collection can be missing the shard key fields. To use findAndModify to set the document's missing shard key:

  • 必须mongos 上运行。请勿直接对分片发出此操作。

  • 如果新的分片键值不是 null,则必须事务中运行或作为可重试写入运行。

  • 必须在完整分片键上包含相等筛选器。

提示

由于缺失的键值是作为 null 相等匹配的一部分返回的,因此为避免更新空值键,请酌情纳入其他查询条件(例如 _id 字段)。

另请参阅:

findAndModify 命令新增了对 bypassDocumentValidation 选项的支持,从而允许您在附带验证规则的集合中插入或更新文档时绕过模式验证

When updating a document, findAndModify and the updateOne() method operate differently:

  • If multiple documents match the update criteria, for findAndModify, you can specify a sort to provide some measure of control on which document to update.

    updateOne() updates the first document that matches.

  • By default, findAndModify returns an object that contains the pre-modified version of the document, as well as the status of the operation. To obtain the updated document, use the new option.

    The updateOne() method returns a WriteResult() object that contains the status of the operation.

    To return the updated document, use the find() method. However, other updates may have modified the document between your update and the document retrieval. Also, if the update modified only a single document but multiple documents matched, you will need to use additional logic to identify the updated document.

When modifying a single document, both findAndModify and the updateOne() method atomically update the document. See 原子性和事务 for more details about interactions and order of operations of these methods.

另请参阅:

findAndModify 可以在分布式事务中使用。

重要

在大多数情况下,与单文档写入操作相比,分布式事务会产生更高的性能成本,并且分布式事务的可用性不应取代有效的模式设计。在许多情况下,非规范化数据模型(嵌入式文档和数组)仍然是数据和使用案例的最佳选择。换言之,对于许多场景,适当的数据建模将最大限度地减少对分布式事务的需求。

有关其他事务使用注意事项(如运行时间限制和 oplog 大小限制),另请参阅生产注意事项

如果分布式事务不是跨分片写入事务,则可以在该事务中创建集合和索引。

具有 upsert: truefindAndModify 可以在现有集合或不存在的集合上运行。如果在不存在的集合上运行,该操作将创建集合。

另请参阅:

如果是在事务中运行,则请勿显式设置此操作的写关注。要将写关注与事务一起使用,请参阅事务和写关注。

The following command updates an existing document in the people collection where the document matches the query criteria:

db.runCommand(
{
findAndModify: "people",
query: { name: "Tom", state: "active", rating: { $gt: 10 } },
sort: { rating: 1 },
update: { $inc: { score: 1 } }
}
)

This command performs the following actions:

  1. The query finds a document in the people collection where the name field has the value Tom, the state field has the value active and the rating field has a value greater than 10.

  2. The sort orders the results of the query in ascending order. If multiple documents meet the query condition, the command will select for modification the first document as ordered by this sort.

  3. The update increments the value of the score field by 1.

  4. 此命令将返回一个包含以下字段的文档:

    • The lastErrorObject field that contains the details of the command, including the field updatedExisting which is true, and

    • The value field that contains the original (i.e. pre-modification) document selected for this update:

      {
      "lastErrorObject" : {
      "connectionId" : 1,
      "updatedExisting" : true,
      "n" : 1,
      "syncMillis" : 0,
      "writtenTo" : null,
      "err" : null,
      "ok" : 1
      },
      value" : {
      "_id" : ObjectId("54f62d2885e4be1f982b9c9c"),
      "name" : "Tom",
      "state" : "active",
      "rating" : 100,
      "score" : 5
      },
      "ok" : 1
      }

To return the updated document in the value field, add the new:true option to the command.

If no document match the query condition, the command returns a document that contains null in the value field:

{ "value" : null, "ok" : 1 }

mongosh and many drivers provide a findAndModify() helper method. Using the shell helper, this previous operation can take the following form:

db.people.findAndModify( {
query: { name: "Tom", state: "active", rating: { $gt: 10 } },
sort: { rating: 1 },
update: { $inc: { score: 1 } }
} );

However, the findAndModify() shell helper method returns only the unmodified document, or if new is true, the updated document.

{
"_id" : ObjectId("54f62d2885e4be1f982b9c9c"),
"name" : "Tom",
"state" : "active",
"rating" : 100,
"score" : 5
}

The following findAndModify command includes the upsert: true option for the update operation to either update a matching document or, if no matching document exists, create a new document:

db.runCommand(
{
findAndModify: "people",
query: { name: "Gus", state: "active", rating: 100 },
sort: { rating: 1 },
update: { $inc: { score: 1 } },
upsert: true
}
)

If the command finds a matching document, the command performs an update.

If the command does not find a matching document, the update with upsert: true operation results in an insertion and returns a document with the following fields:

  • The lastErrorObject field that contains the details of the command, including the field upserted that contains the _id value of the newly inserted document, and

  • The value field containing null.

{
"value" : null,
"lastErrorObject" : {
"updatedExisting" : false,
"n" : 1,
"upserted" : ObjectId("54f62c8bc85d4472eadea26f")
},
"ok" : 1
}

The following findAndModify command includes both upsert: true option and the new:true option. The command either updates a matching document and returns the updated document or, if no matching document exists, inserts a document and returns the newly inserted document in the value field.

In the following example, no document in the people collection matches the query condition:

db.runCommand(
{
findAndModify: "people",
query: { name: "Pascal", state: "active", rating: 25 },
sort: { rating: 1 },
update: { $inc: { score: 1 } },
upsert: true,
new: true
}
)

The command returns the newly inserted document in the value field:

{
"lastErrorObject" : {
"connectionId" : 1,
"updatedExisting" : false,
"upserted" : ObjectId("54f62bbfc85d4472eadea26d"),
"n" : 1,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
},
"value" : {
"_id" : ObjectId("54f62bbfc85d4472eadea26d"),
"name" : "Pascal",
"rating" : 25,
"state" : "active",
"score" : 1
},
"ok" : 1
}

By including a sort specification on the rating field, the following example removes from the people collection a single document with the state value of active and the lowest rating among the matching documents:

db.runCommand(
{
findAndModify: "people",
query: { state: "active" },
sort: { rating: 1 },
remove: true
}
)

The command returns the deleted document:

{
"lastErrorObject" : {
"connectionId" : 1,
"n" : 1,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
},
"value" : {
"_id" : ObjectId("54f62a6785e4be1f982b9c9b"),
"name" : "XYZ123",
"score" : 1,
"state" : "active",
"rating" : 3
},
"ok" : 1
}

排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号规则。

集合 myColl 包含以下文档:

{ _id: 1, category: "café", status: "A" }
{ _id: 2, category: "cafe", status: "a" }
{ _id: 3, category: "cafE", status: "a" }

以下操作包括排序规则选项:

db.runCommand(
{
findAndModify: "myColl",
query: { category: "cafe", status: "a" },
sort: { category: 1 },
update: { $set: { status: "Updated" } },
collation: { locale: "fr", strength: 1 }
}
)

该操作将返回以下文档:

{
"lastErrorObject" : {
"updatedExisting" : true,
"n" : 1
},
"value" : {
"_id" : 1,
"category" : "café",
"status" : "A"
},
"ok" : 1
}

注意

arrayFilters不适用于使用聚合管道的更新。

更新数组字段时,您可以指定 arrayFilters 确定要更新哪些数组元素。

注意

arrayFilters不适用于使用聚合管道的更新。

使用以下文档创建集合 students

db.students.insertMany( [
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
] )

To update all elements that are greater than or equal to 100 in the grades array, use the positional $[<identifier>] operator with the arrayFilters option:

db.runCommand(
{
findAndModify: "students",
query: { grades: { $gte: 100 } },
update: { $set: { "grades.$[element]" : 100 } },
arrayFilters: [ { "element": { $gte: 100 } } ]
}
)

该操作更新单个文档的 grades 字段,完成该操作后,集合中将包含以下文档:

{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 100 ] }
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }

注意

arrayFilters不适用于使用聚合管道的更新。

使用以下文档创建集合 students2

db.students2.insertMany( [
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
] )

The following operation finds a document where the _id field equals 1 and uses the filtered positional operator $[<identifier>] with the arrayFilters to update the mean for all elements in the grades array where the grade is greater than or equal to 85.

db.runCommand(
{
findAndModify: "students2",
query: { _id : 1 },
update: { $set: { "grades.$[elem].mean" : 100 } },
arrayFilters: [ { "elem.grade": { $gte: 85 } } ]
}
)

该操作更新单个文档的 grades 字段,完成该操作后,集合中将包含以下文档:

{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 100, "std" : 4 },
{ "grade" : 85, "mean" : 100, "std" : 6 }
]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}

findAndModify can accept an aggregation pipeline for the update. The pipeline can consist of the following stages:

使用聚合分析管道可以进行更具表现力的更新声明,例如基于当前字段值的Express条件更新或使用另一个字段的值更新一个字段。

例如,使用以下文档创建名为 students2 的集合:

db.students2.insertMany( [
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
] )

以下操作查找 _id 字段等于 1 的文档,并使用聚合管道根据 grades 字段计算新字段 total 的值:

db.runCommand(
{
findAndModify: "students2",
query: { "_id" : 1 },
update: [ { $set: { "total" : { $sum: "$grades.grade" } } } ],
new: true
}
)

注意

管道中使用的 $set 指的是聚合阶段 $set 而不是更新操作符 $set

操作完成后,集合有以下文档:

{
"_id" : 1,
"grades" : [ { "grade" : 80, "mean" : 75, "std" : 6 }, { "grade" : 85, "mean" : 90, "std" : 4 }, { "grade" : 85, "mean" :85, "std" : 6 } ],
"total" : 250
}
{
"_id" : 2,
"grades" : [ { "grade" : 90, "mean" : 75, "std" : 6 }, { "grade" : 87, "mean" : 90, "std" : 3 }, { "grade" : 85, "mean" : 85,"std" : 4 } ]
}

mongosh 中,创建一个 members 集合,其中包含以下文档:

db.members.insertMany( [
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
{ "_id" : 4, "member" : "pqr123", "status" : "D", "points" : 20, "misc1" : "Deactivated", "misc2" : null },
{ "_id" : 5, "member" : "ijk123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
{ "_id" : 6, "member" : "cde123", "status" : "A", "points" : 86, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" }
] )

在集合上创建以下索引:

db.members.createIndex( { status: 1 } )
db.members.createIndex( { points: 1 } )

The following operation explicitly hints to use the index { status: 1 }:

db.runCommand({
findAndModify: "members",
query: { "points": { $lte: 20 }, "status": "P" },
remove: true,
hint: { status: 1 }
})

注意

如果指定不存在的索引,则操作出错。

要查看使用的索引,请对操作运行 explain

db.runCommand(
{
explain: {
findAndModify: "members",
query: { "points": { $lte: 20 }, "status": "P" },
remove: true,
hint: { status: 1 }
},
verbosity: "queryPlanner"
}
)

版本 5.0 中的新增功能

要定义可在命令中其他位置访问的变量,请使用 let 选项。

注意

要使用变量筛选结果,您必须在 $expr 操作符中访问该变量。

创建集合 cakeFlavors

db.cakeFlavors.insertMany( [
{ _id: 1, flavor: "chocolate" },
{ _id: 2, flavor: "strawberry" },
{ _id: 3, flavor: "cherry" }
] )

The following example defines a targetFlavor variable in let and uses the variable to change the cake flavor from cherry to orange:

db.cakeFlavors.runCommand( {
findAndModify: db.cakeFlavors.getName(),
query: { $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
update: { flavor: "orange" },
let: { targetFlavor: "cherry" }
} )