db.collection.updateOne()

    在本页面

    • 定义
    • 语法
    • 访问控制
    • 行为
    • 例子

      定义

    • db.collection. updateOne(过滤,更新,选项)

      version 3.2 中的新内容。

      根据过滤器更新集合中的单个文档。

      语法

    updateOne()方法具有以下形式:

    1. db.collection.updateOne(
    2. <filter>,
    3. <update>,
    4. {
    5. upsert: <boolean>,
    6. writeConcern: <document>,
    7. collation: <document>,
    8. arrayFilters: [ <filterdocument1>, ... ]
    9. }
    10. )

    参数

    db.collection.updateOne()方法采用以下参数:

    参数 类型 描述
    filter document 更新的选择标准。可以使用与find()方法相同的query selectors。
    指定一个空文档{ }以更新集合中的所有文档。
    update document 要应用的修改。可以是以下之一:
    1. 更新文件:仅包含更新运算符表达式。有关更多信息,请参见 使用更新运算符表达式文档进行更新。
    2. 聚合管道(从MongoDB 4.2开始):仅包含以下聚合阶段:
    a. $addFields及其别名 $set
    b. $project及其别名 $unset
    c. replaceRoot及其别名$replaceWith
    有关更多信息,请参见 使用聚合管道更新。
    要使用替换文档进行更新,请参阅 db.collection.replaceOne()
    upsert boolean 可选的。当true,updateOne()时:
    1. 如果没有文档匹配filter,则创建一个新文档。有关详细信息,请参阅upsert 行为。
    2. 更新匹配filter的文档。
    要避免多次 upsert,请确保filter字段为唯一索引。
    默认为false
    writeConcern document 可选的。表示写关注的文件。省略使用默认写入问题。
    如果在事务中运行,请不要为操作明确设置写关注点。要对事务使用写关注,请参见 事务和写关注。
    collation document 可选的。
    指定要用于操作的排序规则。
    排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大写和重音符号的规则。
    排序规则选项具有以下语法:
    collation:{
    locale:<string>,
    caseLevel:<boolean>,
    caseFirst:<string>,
    strength:<int>,
    numericOrdering:<boolean>,
    alternate:<string>,
    maxVariable:<string>,
    backwards :<boolean>
    }
    指定排序规则时,locale字段是必填字段;所有其他排序规则字段都是可选的。有关字段的说明,请参阅整理文件。
    如果未指定排序规则但集合具有默认排序规则(请参阅db.createCollection()),则操作将使用为集合指定的排序规则。
    如果没有为集合或操作指定排序规则,则MongoDB会将以前版本中使用的简单二进制比较用于字符串比较。
    您不能为一个操作指定多个排序规则。例如,您不能为每个字段指定不同的排序规则,或者如果对排序执行查找,则不能对查找使用一种排序规则,而对排序使用另一种排序规则。
    3.4版的新功能。
    arrayFilters array 可选的。过滤器文档的 array,用于确定要在 array 字段上为更新操作修改哪些 array 元素。
    在更新文档中,使用$[<identifier>]过滤后的位置运算符定义一个标识符,然后在数组过滤器文档中引用该标识符。如果更新文档中未包含标识符,则不能具有数组过滤器文档作为标识符。

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

    您可以在更新文档中多次包含相同的标识符;但是,对于更新文档中的每个不同标识符($[identifier]),您必须指定恰好一个对应的 array 过滤器文档。也就是说,您不能为同一标识符指定多个 array 过滤器文档。对于 example,如果 update 语句包含标识符x(可能多次),则不能为arrayFilters指定以下内容,其中包含 2 个单独的x过滤器文档:
    [
    { “x.a”: { $gt: 85 } },
    { “x.b”: { $gt: 80 } }
    ]
    但是,您可以在同一标识符上指定复合条件单个过滤器文档,例如以下示例:
    [
    {$or:[{“x.a”: {$gt: 85}}, {“x.b”: {$gt: 80}}]}
    ]
    [
    {$and:[{“x.a”: {$gt: 85}}, {“x.b”: {$gt: 80}}]}
    ]
    [
    { “x.a”: { $gt: 85 }, “x.b”: { $gt: 80 } }
    ]
    有关示例,请参阅为数组更新操作指定arrayFilters。
    version 3.6 中的新内容。
    hint Document or string 可选的。一个文档或字符串,它指定用于支持查询谓词的索引。
    该选项可以采用索引规范文档或索引名称字符串。
    如果指定的索引不存在,则操作错误。
    有关示例,请参见为更新操作指定提示。
    4.2.1版中的新功能。

    返回: 包含以下内容的文档:
    一个布尔值acknowledged,就好像该操作在运行时带有 写关注关系true或是否禁用了写关注关系false
    matchedCount包含匹配文档数
    modifiedCount包含已修改文档数
    upsertedId包含_id 要提交的文档

    访问控制

    在运行authorization时,用户必须具有包括以下特权的访问权限:

    • update 对指定集合的操作。
    • find对指定集合的操作。
    • insert如果操作导致更新,则对指定的集合执行操作。

    内置角色readWrite提供所需的特权。

    行为

    更新单个文件

    db.collection.updateOne()查找与过滤器匹配的第一个文档,并应用指定的 更新修改。

    使用更新运算符表达式文档进行更新

    对于更新规范,db.collection.updateOne()方法可以接受仅包含更新运算符表达式的文档。

    例如:

    1. db.collection.updateOne(
    2. <query>,
    3. { $set: { status: "D" }, $inc: { quantity: 2 } },
    4. ...
    5. )

    使用聚合管道进行更新

    从MongoDB 4.2开始,db.collection.updateOne()方法可以接受指定要执行的修改的聚合管道 。管道可以包括以下阶段:[ <stage1>, <stage2>, ... ]

    • $addFields及其别名 $set
    • $project及其别名 $unset
    • $replaceRoot及其别名$replaceWith

    使用聚合管道可以实现更具表达力的更新语句,例如根据当前字段值表达条件更新,或使用另一个字段的值更新一个字段。

    例如:

    1. db.collection.updateOne(
    2. <query>,
    3. [
    4. { $set: { status: "Modified", comments: [ "$misc1", "$misc2" ] } },
    5. { $unset: [ "misc1", "misc2" ] }
    6. ]
    7. ...
    8. )

    注意

    $set$unset在管道中是指聚集阶段$set,并$unset分别,而不是更新的运算符$set$unset

    有关示例,请参见使用聚合管道更新。

    UPSERT

    如果upsert: truefilter没有文档匹配,则 db.collection.updateOne()根据条件filterupdate修改创建一个新文档。请参阅 使用Upsert更新。

    如果在分片集合上指定upsert: true,则必须在filter中包括完整的分片键。有关分片集合db.collection.updateOne()的其他行为,请参见分片集合。

    固定集合

    如果更新操作更改了文档大小,则该操作将失败。

    分片集合

    db.collection.updateOne()在分片集合上使用:

    • 如果未指定upsert: true,则必须在字段_id上包含完全匹配项或将目标指定为单个分片(例如,通过在过滤器中包含分片键)。
    • 如果指定upsert: true,则过滤器 必须包含分片键。

      碎片键修改

    从MongoDB 4.2开始,您可以更新文档的分片键值,除非分片键字段是不可变_id字段。有关更新分片键的详细信息,请参见更改文档的分片键值。

    在MongoDB 4.2之前,文档的分片键字段值是不可变的。

    要用于db.collection.updateOne()更新分片键:

    • 必须在运行mongos无论是在事务或作为重试写。千万不能直接在碎片颁发运行。
    • 必须在查询过滤器的完整分片键上包含相等条件。例如,如果一个集合messages 使用{ country : 1, userid : 1 }的片键,更新为一个文件的碎片关键,你必须包括在country: <value>, userid: <value>查询过滤器。您可以根据需要在查询中包括其他字段。

      可解释性

    updateOne()与不兼容 db.collection.explain()

    使用update()代替。

    事务

    db.collection.updateOne()可以在多文档事务中使用。

    重要

    在大多数情况下,与单文档写入相比,多文档事务产生的性能成本更高,并且多文档事务的可用性不应替代有效的架构设计。在许多情况下, 非规范化数据模型(嵌入式文档和数组)将继续是您的数据和用例的最佳选择。也就是说,在许多情况下,适当地对数据建模将最大程度地减少对多文档交易的需求。

    有关其他事务使用方面的注意事项(例如运行时限制和操作日志大小限制),另请参见 生产注意事项。

    现有的集合和事务

    在事务内部,您可以指定对现有集合的读/写操作。如果db.collection.updateOne()导致upsert,则该集合必须已经存在。

    写关注和事务

    如果在事务中运行,请不要为操作明确设置写关注点。要对事务使用写关注,请参见 事务和写关注。

    例子

    更新

    restaurant集合包含以下文档:

    1. { "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan" },
    2. { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
    3. { "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 0 }

    以下操作使用violations字段更新name: "Central Perk Cafe"的单个文档:

    1. try {
    2. db.restaurant.updateOne(
    3. { "name" : "Central Perk Cafe" },
    4. { $set: { "violations" : 3 } }
    5. );
    6. } catch (e) {
    7. print(e);
    8. }

    操作返回:

    1. { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

    如果未找到匹配项,则操作将返回:

    1. { "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }

    如果未找到 match,则设置upsert: true将插入文档。见使用 Upsert 更新

    使用聚合管道更新

    从MongoDB 4.2开始,db.collection.updateOne()可以使用聚合管道进行更新。管道可以包括以下阶段:

    • $addFields及其别名 $set
    • $project及其别名 $unset
    • $replaceRoot及其别名$replaceWith

    使用聚合管道可以实现更具表达力的更新语句,例如根据当前字段值表达条件更新,或使用另一个字段的值更新一个字段。

    实施例1

    以下示例使用聚合管道文档中其他字段的值来修改字段。

    members使用以下文档创建一个集合:

    1. db.members.insertMany([
    2. { "_id" : 1, "member" : "abc123", "status" : "A", "points" : 2, "misc1" : "note to self: confirm status", "misc2" : "Need to activate", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
    3. { "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, comments: [ "reminder: ping me at 100pts", "Some random comment" ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
    4. ])

    假设您希望将这些字段收集到一个新字段中,而不是使用单独的misc1misc2字段comments。以下更新操作使用聚合管道执行以下操作:

    • 添加新comments字段并设置该lastUpdate字段。
    • 删除集合中所有文档的misc1misc2字段。
    1. db.members.updateOne(
    2. { _id: 1 },
    3. [
    4. { $set: { status: "Modified", comments: [ "$misc1", "$misc2" ], lastUpdate: "$$NOW" } },
    5. { $unset: [ "misc1", "misc2" ] }
    6. ]
    7. )

    注意

    $set$unset在管道中是指聚合阶段$set,并$unset分别,而不是更新的运营商$set$unset

    第一阶段

    $set阶段:

    • 创建一个新的数组字段,comments其元素是misc1misc2字段的当前内容
    • 将字段设置为lastUpdate聚合变量的值NOW。聚合变量 NOW解析为当前日期时间值,并且在整个管道中保持不变。要访问聚合变量,请在变量前加双美元符号$$ 并用引号引起来。

    第二阶段

    $unset阶段将删除misc1misc2字段。

    命令后,集合包含以下文档:

    1. { "_id" : 1, "member" : "abc123", "status" : "Modified", "points" : 2, "lastUpdate" : ISODate("2020-01-23T05:21:59.321Z"), "comments" : [ "note to self: confirm status", "Need to activate" ] }
    2. { "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "comments" : [ "reminder: ping me at 100pts", "Some random comment" ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }

    示例

    聚合管道允许基于当前字段值执行条件更新,以及使用当前字段值来计算单独的字段值。

    例如,students3使用以下文档创建一个集合:

    1. db.students3.insert([
    2. { "_id" : 1, "tests" : [ 95, 92, 90 ], "average" : 92, "grade" : "A", "lastUpdate" : ISODate("2020-01-23T05:18:40.013Z") },
    3. { "_id" : 2, "tests" : [ 94, 88, 90 ], "average" : 91, "grade" : "A", "lastUpdate" : ISODate("2020-01-23T05:18:40.013Z") },
    4. { "_id" : 3, "tests" : [ 70, 75, 82 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
    5. ]);

    第三个文档_id: 3缺少averagegrade 字段。使用聚合管道,可以使用计算出的平均成绩和字母成绩更新文档。

    1. db.students3.updateOne(
    2. { _id: 3 },
    3. [
    4. { $set: { average: { $trunc: [ { $avg: "$tests" }, 0 ] }, lastUpdate: "$$NOW" } },
    5. { $set: { grade: { $switch: {
    6. branches: [
    7. { case: { $gte: [ "$average", 90 ] }, then: "A" },
    8. { case: { $gte: [ "$average", 80 ] }, then: "B" },
    9. { case: { $gte: [ "$average", 70 ] }, then: "C" },
    10. { case: { $gte: [ "$average", 60 ] }, then: "D" }
    11. ],
    12. default: "F"
    13. } } } }
    14. ]
    15. )

    注意

    $set管道中的使用是指聚合阶段 $set,而不是更新运算符$set

    第一阶段

    $set阶段:

    • 根据字段average的平均值 计算一个新tests字段。请参阅$avg有关 $avg聚合运算符$trunc的更多信息和有关$trunc截断聚合运算符的更多信息。
    • 将字段设置为lastUpdate聚合变量的值NOW。聚合变量 NOW解析为当前日期时间值,并且在整个管道中保持不变。要访问聚合变量,请在变量前加双美元符号$$ 并用引号引起来。

    第二阶段

    $set阶段计算新字段grade基础上,average在前一阶段计算。参见 $switch以获取有关$switch 聚合运算符的更多信息。

    命令后,集合包含以下文档:

    1. { "_id" : 1, "tests" : [ 95, 92, 90 ], "average" : 92, "grade" : "A", "lastUpdate" : ISODate("2020-01-23T05:18:40.013Z") }
    2. { "_id" : 2, "tests" : [ 94, 88, 90 ], "average" : 91, "grade" : "A", "lastUpdate" : ISODate("2020-01-23T05:18:40.013Z") }
    3. { "_id" : 3, "tests" : [ 70, 75, 82 ], "lastUpdate" : ISODate("2020-01-24T17:33:30.674Z"), "average" : 75, "grade" : "C" }

    也可以看看

    聚合管道更新

    使用 Upsert 更新

    restaurant集合包含以下文档:

    1. { "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 },
    2. { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
    3. { "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : "0" }

    以下操作尝试使用name : "Pizza Rat's Pizzaria"更新文档,而upsert: true

    1. try {
    2. db.restaurant.updateOne(
    3. { "name" : "Pizza Rat's Pizzaria" },
    4. { $set: {"_id" : 4, "violations" : 7, "borough" : "Manhattan" } },
    5. { upsert: true }
    6. );
    7. } catch (e) {
    8. print(e);
    9. }

    upsert:true开始,文档基于filterupdate标准inserted。操作返回:

    1. {
    2. "acknowledged" : true,
    3. "matchedCount" : 0,
    4. "modifiedCount" : 0,
    5. "upsertedId" : 4
    6. }

    该集合现在包含以下文档:

    1. { "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 },
    2. { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
    3. { "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 4 },
    4. { "_id" : 4, "name" : "Pizza Rat's Pizzaria", "Borough" : "Manhattan", "violations" : 7 }

    name字段使用filter条件填充,而update operators 用于创建文档的 rest。

    以下操作使用violations更新大于10的第一个文档:

    1. try {
    2. db.restaurant.updateOne(
    3. { "violations" : { $gt: 10} },
    4. { $set: { "Closed" : true } },
    5. { upsert: true }
    6. );
    7. } catch (e) {
    8. print(e);
    9. }

    操作返回:

    1. {
    2. "acknowledged" : true,
    3. "matchedCount" : 0,
    4. "modifiedCount" : 0,
    5. "upsertedId" : ObjectId("56310c3c0c5cbb6031cafaea")
    6. }

    该集合现在包含以下文档:

    1. { "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 },
    2. { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
    3. { "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 4 },
    4. { "_id" : 4, "name" : "Pizza Rat's Pizzaria", "Borough" : "Manhattan", "grade" : 7 }
    5. { "_id" : ObjectId("56310c3c0c5cbb6031cafaea"), "Closed" : true }

    由于没有文档与过滤器匹配,并且upserttrue,updateOne仅使用生成的_idupdate条件插入文档。

    写关注更新

    给定三个成员副本集,以下操作指定majority wtimeoutwtimeout 100

    1. try {
    2. db.restaurant.updateOne(
    3. { "name" : "Pizza Rat's Pizzaria" },
    4. { $inc: { "violations" : 3}, $set: { "Closed" : true } },
    5. { w: "majority", wtimeout: 100 }
    6. );
    7. } catch (e) {
    8. print(e);
    9. }

    如果主要和至少一个辅助设备在 100 毫秒内确认每个写入操作,则返回:

    1. { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

    如果确认时间超过wtimeout限制,则抛出以下 exception:

    1. WriteConcernError({
    2. "code" : 64,
    3. "errInfo" : {
    4. "wtimeout" : true
    5. },
    6. "errmsg" : "waiting for replication timed out"
    7. }):

    指定排序规则

    version 3.4 中的新内容。

    整理允许用户为 string 比较指定 language-specific 规则,例如字母和重音标记的规则。

    集合myColl具有以下文档:

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

    以下操作包括整理选项:

    1. db.myColl.updateOne(
    2. { category: "cafe" },
    3. { $set: { status: "Updated" } },
    4. { collation: { locale: "fr", strength: 1 } }
    5. );

    为 Array Update Operations 指定 arrayFilters

    version 3.6 中的新内容。

    从 MongoDB 3.6 开始,在更新 array 字段时,您可以指定arrayFilters来确定要更新的 array 元素。

    更新元素 Match arrayFilters Criteria

    使用以下文档创建集合students

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

    要修改grades array 中大于或等于100的所有元素,请使用过滤后的位置 operator $ [<identifier>]和db.collection.updateOne方法中的arrayFilters选项:

    1. db.students.updateOne(
    2. { grades: { $gte: 100 } },
    3. { $set: { "grades.$[element]" : 100 } },
    4. { arrayFilters: [ { "element": { $gte: 100 } } ] }
    5. )

    该操作更新单个文档的grades字段,在操作之后,该集合具有以下文档:

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

    要修改是大于或等于所有元素100grades阵列中,使用过滤的位置操作者 $[<identifier>]arrayFilters在选项 db.collection.updateOne方法:

    1. db.students.updateOne(
    2. { grades: { $gte: 100 } },
    3. { $set: { "grades.$[element]" : 100 } },
    4. { arrayFilters: [ { "element": { $gte: 100 } } ] }
    5. )

    该操作将更新grades单个文档的字段,并且在操作之后,集合具有以下文档:

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

    更新 Array 文档的特定元素

    使用以下文档创建集合students2

    1. db.students2.insert([
    2. {
    3. "_id" : 1,
    4. "grades" : [
    5. { "grade" : 80, "mean" : 75, "std" : 6 },
    6. { "grade" : 85, "mean" : 90, "std" : 4 },
    7. { "grade" : 85, "mean" : 85, "std" : 6 }
    8. ]
    9. },
    10. {
    11. "_id" : 2,
    12. "grades" : [
    13. { "grade" : 90, "mean" : 75, "std" : 6 },
    14. { "grade" : 87, "mean" : 90, "std" : 3 },
    15. { "grade" : 85, "mean" : 85, "std" : 4 }
    16. ]
    17. }
    18. ])

    要修改grades array 中等级大于或等于85的所有元素的mean字段的 value,请使用过滤后的位置 operator $ [<identifier>]和db.collection.updateOne方法中的arrayFilters

    1. db.students2.updateOne(
    2. { },
    3. { $set: { "grades.$[elem].mean" : 100 } },
    4. { arrayFilters: [ { "elem.grade": { $gte: 85 } } ] }
    5. )

    该操作更新单个文档的 array,并且在操作之后,该集合具有以下文档:

    1. {
    2. "_id" : 1,
    3. "grades" : [
    4. { "grade" : 80, "mean" : 75, "std" : 6 },
    5. { "grade" : 85, "mean" : 100, "std" : 4 },
    6. { "grade" : 85, "mean" : 100, "std" : 6 }
    7. ]
    8. }
    9. {
    10. "_id" : 2,
    11. "grades" : [
    12. { "grade" : 90, "mean" : 75, "std" : 6 },
    13. { "grade" : 87, "mean" : 90, "std" : 3 },
    14. { "grade" : 85, "mean" : 85, "std" : 4 }
    15. ]
    16. }

    也可以看看

    要更新多个文档,请参阅db.collection.updateMany()。

    指定hint更新操作

    4.2.1版中的新功能。

    members使用以下文档创建样本集合:

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

    在集合上创建以下索引:

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

    以下更新操作明确暗示要使用索引:{ status: 1 }

    注意

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

    1. db.members.updateOne(
    2. { "points": { $lte: 20 }, "status": "P" },
    3. { $set: { "misc1": "Need to activate" } },
    4. { hint: { status: 1 } }
    5. )

    update命令返回以下内容:

    1. { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

    要查看使用的索引,可以使用$indexStats管道:

    1. db.members.aggregate( [ { $indexStats: { } }, { $sort: { name: 1 } } ] )

    译者:李冠飞

    校对:

    参见

    原文 - db.collection.updateOne()