Mongodb 更新集合的方法到底有几种 (上) ?

更新方法

Mongodb 使用以下几种方法来更新文档 , Mongodb V5.0+ 使用 mongosh 客户端:

  • db.collection.updateOne(, , )

  • db.collection.updateMany(, , )

  • db.collection.replaceOne(, , )

  • db.collection.findOneAndReplace(, , )

  • db.collection.findOneAndUpdate( , , )

  • db.collection.findAndModify()

  • Bulk.find.update()

  • Bulk.find.updateOne()

  • Bulk.find.upsert()

更新单个文档

行为

  • 更新单个文档 db.collection.updateOne()找到第一个匹配的文档筛选, 并应用指定的更新修改。

使用更新操作符表达式文档进行更新

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

为了更新文档,MongoDB 提供了 更新操作符 $set 修改字段值。

要使用更新运算符,请将以下形式的更新文档传递给更新方法:

{
  <update operator>: { <field1>: <value1>, ... },
  <update operator>: { <field2>: <value2>, ... },
  ...
}

updateOne() 方法具有以下语法:

db.collection.updateMany(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        // Available starting in MongoDB 4.2.1
   }
)

例子: 修改以下订单 _id: 1 更新 items 数组第一个元素的 qty 字段为 10 , 并更新 status 为 0 。

更新操作:

  • 使用 $set 运算符将​​字段 items.0.qty 的值更新为 “10”, 并将字段的值更新status 为 “0” 。

  • 使用 $currentDate 运算符将​​字段 lastModified 的值更新为当前日期。如果 lastModified 字段不存在, $currentDate将创建该字段。

该方法返回一个包含以下内容的文档:

  • acknowledged:布尔值,true 表示带写关注运行,否则为 false。

  • matchedCount:包含匹配文档的数量。

  • modifiedCount:包含修改文档的数量。

  • upsertedId: 包含_id更新插入文档的。

# 查询当前 _id: 1  的订单记录
sit_rs1:PRIMARY> db.orders.find({"_id": 1}).pretty()
{
        "_id" : 1,
        "cust_id" : "A",
        "ord_date" : ISODate("2023-06-01T00:00:00Z"),
        "price" : 15,
        "items" : [
                {
                        "sku" : "apple",
                        "qty" : 5,
                        "price" : 2.5
                },
                {
                        "sku" : "apples",
                        "qty" : 5,
                        "price" : 2.5
                }
        ],
        "status" : "1"
}

# 更新 items 数组第一个元素的 qty 字段为 10 
sit_rs1:PRIMARY> db.orders.updateOne(
...    { "_id": 1 },
...    {
...      $set: { "items.0.qty": "10", status: "0" },
...      $currentDate: { lastModified: true }
...    }
... )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }


# 检查更新完成
sit_rs1:PRIMARY> db.orders.find({"_id": 1}).pretty()
{
        "_id" : 1,
        "cust_id" : "A",
        "ord_date" : ISODate("2023-06-01T00:00:00Z"),
        "price" : 15,
        "items" : [
                {
                        "sku" : "apple",
                        "qty" : "10",
                        "price" : 2.5
                },
                {
                        "sku" : "apples",
                        "qty" : 5,
                        "price" : 2.5
                }
        ],
        "status" : "0",
        "lastModified" : ISODate("2023-08-11T05:28:58.975Z")
}

主键 _id 更新, 一旦设置后,您将无法更新该字段的值_id,也无法使用具有不同 _id 字段值的文档替换现有文档。

你将会收到下面的错误信息:如下:

sit_rs1:PRIMARY> db.orders.updateOne(
...    { "_id": 1 },
...    {
...      $set: { "_id": "111", status: "0" },
...      $currentDate: { lastModified: true }
...    }
... )
WriteError({
        "index" : 0,
        "code" : 66,
        "errmsg" : "Performing an update on the path '_id' would modify the immutable field '_id'",
        "op" : {
                "q" : {
                        "_id" : 1
                },
                "u" : {
                        "$set" : {
                                "_id" : "111",
                                "status" : "0"
                        },
                        "$currentDate" : {
                                "lastModified" : true
                        }
                },
                "multi" : false,
                "upsert" : false
        }
}) :
WriteError({
        "index" : 0,
        "code" : 66,
        "errmsg" : "Performing an update on the path '_id' would modify the immutable field '_id'",
        "op" : {
                "q" : {
                        "_id" : 1
                },
                "u" : {
                        "$set" : {
                                "_id" : "111",
                                "status" : "0"
                        },
                        "$currentDate" : {
                                "lastModified" : true
                        }
                },
                "multi" : false,
                "upsert" : false
        }
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.updateOne@src/mongo/shell/crud_api.js:600:17
@(shell):1:1

使用 upsert 选修的。当为 true, 如果没有与筛选器匹配的文档,则创建新文档。如果匹配,则更新与筛选器匹配的单个文档。为了避免多次反转,请确保过滤器字段是唯一索引。默认为false,在没有找到匹配项时不插入新文档。

# 查找  "_id": 11 记录为空
sit_rs1:PRIMARY> db.orders.findOne({ "_id": 11 })
null

# 如果没有与筛选器匹配的文档,则创建新文档, "upsertedId" : 11 返回主键 ID 值
sit_rs1:PRIMARY> try {
...    db.orders.updateOne(
...       { "_id": 11 },
...       { $set: {"_id" : 11, "cust_id" : "B",  "price" : 20 } },
...       { upsert: true }
...    );
... } catch (e) {
...    print(e);
... }
{
        "acknowledged" : true,
        "matchedCount" : 0,
        "modifiedCount" : 0,
        "upsertedId" : 11
}

# 检查记录已插入
sit_rs1:PRIMARY> db.orders.findOne({ "_id": 11 })
{ "_id" : 11, "cust_id" : "B", "price" : 20 }


# 第二次执行,由于存在记录,则更新匹配的文档 "modifiedCount" : 1 
sit_rs1:PRIMARY> try {
...    db.orders.updateOne(
...       { "_id": 11 },
...       { $set: {"_id" : 11, "cust_id" : "A",  "price" : 10 } },
...       { upsert: true }
...    );
... } catch (e) {
...    print(e);
... }
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }


# 检查记录已更新
sit_rs1:PRIMARY> db.orders.findOne({ "_id": 11 })
{ "_id" : 11, "cust_id" : "A", "price" : 10 }

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

  • $addFields 及其别名 $set

  • $project 及其别名 $unset

  • $replaceRoot 及其别名 $replaceWith

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

示例,合并 members 集合的爱好,并删除 hobby1 和 hobby2 字段:

  • $set 阶段: 创建一个新的数组字段 hobby ,其元素是 hobby1 、hobby2 字段合并,将字段设置 lastUpdate 为聚合变量的值NOW。聚合变量 NOW 解析为当前日期时间值,并在整个管道中保持不变。要访问聚合变量,请在变量前加上双美元符号 $$ 并用引号引起来。
  • $unset 阶段删除了 hobby1 和 hobby2 字段。
sit_rs1:PRIMARY> db.members.find() 
{ "_id" : 1, "member" : "abc", "status" : "1", "hobby1" : "football", "hobby2" : "basketball", "lastUpdate" : ISODate("2023-08-14T15:00:00Z") }
{ "_id" : 2, "member" : "xyz", "status" : "1", "hobby" : [ "Ping Pong", "swimming" ], "lastUpdate" : ISODate("2023-08-14T15:30:00Z") }
 
sit_rs1:PRIMARY> db.members.updateOne(
...    { _id: 1 },
...    [
...       { $set: { status: "Modified", hobby: [ "$hobby1", "$hobby2" ], lastUpdate: "$$NOW" } },
...       { $unset: [ "hobby1", "hobby2" ] }
...    ]
... )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

sit_rs1:PRIMARY> db.members.find()
{ "_id" : 1, "member" : "abc", "status" : "Modified", "lastUpdate" : ISODate("2023-08-14T07:38:15.198Z"), "hobby" : [ "football", "basketball" ] }
{ "_id" : 2, "member" : "xyz", "status" : "1", "hobby" : [ "Ping Pong", "swimming" ], "lastUpdate" : ISODate("2023-08-14T15:30:00Z") }

更新多个文档

行为

  • updateMany() 更新集合中与 匹配的所有匹配文档filter,使用update条件应用修改。

更新插入

  • 如果upsert: true没有文档匹配filter, db.collection.updateMany()根据filter和update参数创建一个新文档。

updateMany() 更新与集合的指定过滤器匹配的所有文档。updateMany() 方法具有以下形式:

db.collection.updateMany(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        // Available starting in MongoDB 4.2.1
   }
)

以下更新订单_id 小于5的记录,修改其items数组中第一个元素 qty 为15, 修改 status 状态为 “0” , 同时增加 lastModified 字段,刷新更新时间,如下:


# 当前订单表状态
sit_rs1:PRIMARY> db.orders.find({}, {_id:1, items:1, status:1, lastModified:1}).sort({"_id":1})
{ "_id" : 1, "items" : [ { "sku" : "apple", "qty" : "10", "price" : 2.5 }, { "sku" : "apples", "qty" : 5, "price" : 2.5 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T05:28:58.975Z") }
{ "_id" : 2, "items" : [ { "sku" : "apple", "qty" : 8, "price" : 2.5 }, { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 3, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 }, { "sku" : "pears", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 4, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 5, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 6, "items" : [ { "sku" : "carrots", "qty" : 10, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 7, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 8, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 9, "items" : [ { "sku" : "carrots", "qty" : 5, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 }, { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 10, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }


# 更新订单_id 小于 5 的记录
sit_rs1:PRIMARY> db.orders.updateMany(
...    { "_id": { $lt: 5 } },
...    {
...      $set: { "items.0.qty": "15", status: "0" },
...      $currentDate: { lastModified: true }
...    }
... )
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 4 }


# 检查结果 
sit_rs1:PRIMARY> db.orders.find({}, {_id:1, items:1, status:1, lastModified:1}).sort({"_id":1})
{ "_id" : 1, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 }, { "sku" : "apples", "qty" : 5, "price" : 2.5 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.781Z") }
{ "_id" : 2, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 }, { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.782Z") }
{ "_id" : 3, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 }, { "sku" : "pears", "qty" : 10, "price" : 2.5 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.782Z") }
{ "_id" : 4, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.782Z") }
{ "_id" : 5, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 6, "items" : [ { "sku" : "carrots", "qty" : 10, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 7, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 8, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 9, "items" : [ { "sku" : "carrots", "qty" : 5, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 }, { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 10, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }

关于 updateMany() 其它参数的使用,请参照上面的 updateOne() , 比如 upsert 操作。

另外,如果需要对数组更新,可以指定arrayFilters数组更新操作, 从 MongoDB 3.6 开始,更新数组字段时,您可以指定arrayFilters确定要更新哪些数组元素。

以下示例,更新数组 number 元素,字段 n1 的值,如果>=8, 则更新为 99. 如下:

sit_rs1:PRIMARY> db.user.find().sort({age:1})
{ "_id" : ObjectId("64b8d2404b442dde59447cf0"), "name" : "user1", "age" : 10, "item" : { "A" : 111, "B" : 211 }, "number" : [ { "n1" : 2, "n2" : 6 }, { "n1" : 3, "n2" : 3 }, { "n1" : 5, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf1"), "name" : "user2", "age" : 20, "item" : { "A" : 112, "B" : 212 }, "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 4, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf2"), "name" : "user3", "age" : 30, "item" : { "A" : 113, "B" : 213 }, "number" : [ { "n1" : 1, "n2" : 8 }, { "n1" : 2, "n2" : 6 }, { "n1" : 2, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf6"), "name" : "user7", "age" : 45, "item" : { "A" : 116, "B" : 216 }, "number" : [ { "n1" : 6, "n2" : 6 }, { "n1" : 8, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf3"), "name" : "user4", "age" : 45, "item" : { "A" : 111, "B" : 211 }, "number" : [ { "n1" : 9, "n2" : 6 }, { "n1" : 2, "n2" : 4 }, { "n1" : 3, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 55, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 8, "n2" : 4 }, { "n1" : 4, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf5"), "name" : "user22", "age" : 55, "item" : { "A" : 115, "B" : 212 }, "number" : [ { "n1" : 5, "n2" : 5 }, { "n1" : 8, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "age" : 60, "item" : { "A" : 113, "B" : 211 }, "number" : [ { "n1" : 5, "n2" : 4 }, { "n1" : 8, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf8"), "name" : "user9", "age" : 75, "item" : { "A" : 118, "B" : 218 }, "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 8, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }

sit_rs1:PRIMARY> db.user.updateMany(
...    { },
...    { $set: { "number.$[elem].n1" : 99 } },
...    { arrayFilters: [ { "elem.n1": { $gte: 8 } } ] }
... )
{ "acknowledged" : true, "matchedCount" : 9, "modifiedCount" : 6 }

sit_rs1:PRIMARY> db.user.find().sort({age:1})
{ "_id" : ObjectId("64b8d2404b442dde59447cf0"), "name" : "user1", "age" : 10, "item" : { "A" : 111, "B" : 211 }, "number" : [ { "n1" : 2, "n2" : 6 }, { "n1" : 3, "n2" : 3 }, { "n1" : 5, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf1"), "name" : "user2", "age" : 20, "item" : { "A" : 112, "B" : 212 }, "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 4, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf2"), "name" : "user3", "age" : 30, "item" : { "A" : 113, "B" : 213 }, "number" : [ { "n1" : 1, "n2" : 8 }, { "n1" : 2, "n2" : 6 }, { "n1" : 2, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf6"), "name" : "user7", "age" : 45, "item" : { "A" : 116, "B" : 216 }, "number" : [ { "n1" : 6, "n2" : 6 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf3"), "name" : "user4", "age" : 45, "item" : { "A" : 111, "B" : 211 }, "number" : [ { "n1" : 99, "n2" : 6 }, { "n1" : 2, "n2" : 4 }, { "n1" : 3, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 55, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf5"), "name" : "user22", "age" : 55, "item" : { "A" : 115, "B" : 212 }, "number" : [ { "n1" : 5, "n2" : 5 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "age" : 60, "item" : { "A" : 113, "B" : 211 }, "number" : [ { "n1" : 5, "n2" : 4 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf8"), "name" : "user9", "age" : 75, "item" : { "A" : 118, "B" : 218 }, "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }

替换单个文档

根据过滤器替换集合中的单个文档。

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

db.collection.replaceOne(
   <filter>,
   <replacement>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     hint: <document|string>                   // Available starting in 4.2.1
   }
)

行为

  • replaceOne() :使用文档替换集合中与 filter匹配的第一个匹配文档 replacement 。

  • upsert 参数: 如果upsert: true没有文档匹配filter, db.collection.replaceOne() 根据该replacement文档创建一个新文档。

  • 注意: 替换是整行替换,而不是替换字段。

sit_rs1:PRIMARY> db.user.find().sort({name:1})
{ "_id" : ObjectId("64b8d2404b442dde59447cf1"), "name" : "user2", "age" : 20, "item" : { "A" : 112, "B" : 212 }, "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 4, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf5"), "name" : "user22", "age" : 55, "item" : { "A" : 115, "B" : 212 }, "number" : [ { "n1" : 5, "n2" : 5 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf2"), "name" : "user3", "age" : 30, "item" : { "A" : 113, "B" : 213 }, "number" : [ { "n1" : 1, "n2" : 8 }, { "n1" : 2, "n2" : 6 }, { "n1" : 2, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf3"), "name" : "user4", "age" : 45, "item" : { "A" : 111, "B" : 211 }, "number" : [ { "n1" : 99, "n2" : 6 }, { "n1" : 2, "n2" : 4 }, { "n1" : 3, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 55, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf6"), "name" : "user7", "age" : 45, "item" : { "A" : 116, "B" : 216 }, "number" : [ { "n1" : 6, "n2" : 6 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "age" : 60, "item" : { "A" : 113, "B" : 211 }, "number" : [ { "n1" : 5, "n2" : 4 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf8"), "name" : "user9", "age" : 75, "item" : { "A" : 118, "B" : 218 }, "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }

# 用户 user2 存在, 整行被替换 !!!
sit_rs1:PRIMARY> try {
...    db.user.replaceOne(
...       { "name" : "user2" },
...       { "name" : "user1", status: "Modified" }, 
...    { upsert: true }
...    );
... } catch (e){
...    print(e);
... }
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

sit_rs1:PRIMARY> db.user.find().sort({name:1})
{ "_id" : ObjectId("64b8d2404b442dde59447cf1"), "name" : "user1", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf5"), "name" : "user22", "age" : 55, "item" : { "A" : 115, "B" : 212 }, "number" : [ { "n1" : 5, "n2" : 5 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf2"), "name" : "user3", "age" : 30, "item" : { "A" : 113, "B" : 213 }, "number" : [ { "n1" : 1, "n2" : 8 }, { "n1" : 2, "n2" : 6 }, { "n1" : 2, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf3"), "name" : "user4", "age" : 45, "item" : { "A" : 111, "B" : 211 }, "number" : [ { "n1" : 99, "n2" : 6 }, { "n1" : 2, "n2" : 4 }, { "n1" : 3, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 55, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf6"), "name" : "user7", "age" : 45, "item" : { "A" : 116, "B" : 216 }, "number" : [ { "n1" : 6, "n2" : 6 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "age" : 60, "item" : { "A" : 113, "B" : 211 }, "number" : [ { "n1" : 5, "n2" : 4 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf8"), "name" : "user9", "age" : 75, "item" : { "A" : 118, "B" : 218 }, "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }


# 用户 user10 不存在, 因为使用 upsert 参数,插入新文档 
sit_rs1:PRIMARY> try {
...    db.user.replaceOne(
...       { "name" : "user10" },
...       { "name" : "user10", status: "Modified" }, 
...    { upsert: true }
...    );
... } catch (e){
...    print(e);
... }
{
        "acknowledged" : true,
        "matchedCount" : 0,
        "modifiedCount" : 0,
        "upsertedId" : ObjectId("64d9f6bb53d3e4e23aa85489")
}

# user10 不存在 插入文档
sit_rs1:PRIMARY> db.user.find().sort({name:1})
{ "_id" : ObjectId("64b8d2404b442dde59447cf1"), "name" : "user1", "status" : "Modified" }
{ "_id" : ObjectId("64d9f6bb53d3e4e23aa85489"), "name" : "user10", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf5"), "name" : "user22", "age" : 55, "item" : { "A" : 115, "B" : 212 }, "number" : [ { "n1" : 5, "n2" : 5 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf2"), "name" : "user3", "age" : 30, "item" : { "A" : 113, "B" : 213 }, "number" : [ { "n1" : 1, "n2" : 8 }, { "n1" : 2, "n2" : 6 }, { "n1" : 2, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf3"), "name" : "user4", "age" : 45, "item" : { "A" : 111, "B" : 211 }, "number" : [ { "n1" : 99, "n2" : 6 }, { "n1" : 2, "n2" : 4 }, { "n1" : 3, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 55, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf6"), "name" : "user7", "age" : 45, "item" : { "A" : 116, "B" : 216 }, "number" : [ { "n1" : 6, "n2" : 6 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "age" : 60, "item" : { "A" : 113, "B" : 211 }, "number" : [ { "n1" : 5, "n2" : 4 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b8d2404b442dde59447cf8"), "name" : "user9", "age" : 75, "item" : { "A" : 118, "B" : 218 }, "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }

你可能感兴趣的:(MongoDB,mongodb,数据库)