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

这篇具有很好参考价值的文章主要介绍了Mongodb 更新集合的方法到底有几种 (下) ?。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

更新方法

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

  • db.collection.updateOne(<filter>, <update>, <options>)

  • db.collection.updateMany(<filter>, <update>, <options>)

  • db.collection.replaceOne(<filter>, <update>, <options>)

  • db.collection.findOneAndReplace(<filter>, <replacement>, <options>)

  • db.collection.findOneAndUpdate( <filter>, <update>, <options>)

  • db.collection.findAndModify(<document>)

  • db.collection.bulkWrite()

  • Bulk.find.replaceOne(<document>)

  • Bulk.find.update(<update>)

  • Bulk.find.updateOne(<update>)

  • Bulk.find.upsert()

批量写入

通过控制执行顺序执行多个写入操作。

bulkWrite() 接受一组写操作并执行每个操作。默认情况下,操作按顺序执行。

每组的操作数量不能超过数据库的maxWriteBatchSize的值。

maxWriteBatchSize的 默认值为100,000。该值显示在 hello.maxWriteBatchSize字段中。

sit_rs1:PRIMARY> rs.hello().maxWriteBatchSize
100000

db.collection.bulkWrite() 具有以下语法:

db.collection.bulkWrite(
   [ <operation 1>, <operation 2>, ... ],
   {
      writeConcern : <document>,
      ordered : <boolean>
   }
)

operations: 一个数组 bulkWrite() 的写操作。 有效的操作是:

  • insertOne

  • updateOne

  • updateMany

  • deleteOne

  • deleteMany

  • replaceOne

该 ordered 参数指定是否 bulkWrite() 将按顺序或不按顺序执行操作。默认情况下,操作按顺序执行。

下面的代码代表了一个 bulkWrite() 有五个操作。

db.collection.bulkWrite(
   [
      { insertOne : <document> },
      { updateOne : <document> },
      { updateMany : <document> },
      { replaceOne : <document> },
      { deleteOne : <document> },
      { deleteMany : <document> }
   ]
)

默认 ordered : true 状态下,每个操作都会按顺序执行,从第一个操作 insertOne 到最​​后一个操作 deleteMany。

如果 ordered 设置为 false,则 mongod 可以对操作重新排序以提高性能。应用程序不应依赖于操作执行的顺序。

使用 时 ordered : false,操作结果可能会有所不同 !!!

以下示例向集合 user 插入 2 条 新记录 _id: 3, _id: 4, 更新 name: “user5” 用户,设置 age 等于57 , 删除 user10, user11 用户( 这里用了 deleteOne 方法,只会删除第一条匹配)。 最后再替换 name: “user8” 的文档 记录。 如下:

sit_rs1:PRIMARY> db.user.find()
{ "_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("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("64b8d2404b442dde59447cf8"), "name" : "user10", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "age" : 61, "item" : { "A" : 113, "B" : 211 }, "number" : [ { "n1" : 5, "n2" : 4 }, { "n1" : 99, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }


# 插入2条新记录,更新1条,删除1条,替换1条
sit_rs1:PRIMARY> try {
...    db.user.bulkWrite( [
...       { insertOne: { document: { _id: 3, name: "user20", city: "sz", age: 26 } } },
...       { insertOne: { document: { _id: 4, name: "user21", city: "bj", age: 30 } } },
...       { updateOne: {
...          filter: { name: "user5" },
...          update: { $set: { age: 57 } }
...       } },
...       { deleteOne: { filter: { name: { $in: ["user10", "user11" ] } } } },
...       { replaceOne: {
...          filter: { name: "user8" },
...          replacement: { name: "user8", city: "sh", age: 66 }
...       } }
...    ] )
... } catch( error ) {
...    print( error )
... }
{
        "acknowledged" : true,
        "deletedCount" : 1,
        "insertedCount" : 2,
        "matchedCount" : 2,
        "upsertedCount" : 0,
        "insertedIds" : {
                "0" : 3,
                "1" : 4
        },
        "upsertedIds" : {

        }
}

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "city" : "sh", "age" : 66 }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : 26 }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

Bulk 构建器

Bulk.find.replaceOne(<document>) 将单个文档替换操作添加到批量操作列表中。使用该 Bulk.find() 方法指定确定要替换哪个文档的条件。Bulk.find.replaceOne() 方法将替换限制为单个文档。

Bulk() 批量操作构建器用于构造要为单个集合批量执行的写入操作列表。

要实例化构建器,请使用

  • 有序批量操作: db.collection.initializeOrderedBulkOp()
  • 无序批量操作: db.collection.initializeUnorderedBulkOp()。

有序操作

  • 通过有序操作列表,MongoDB 会串行执行列表中的写入操作。如果在处理其中一个写操作期间发生错误,MongoDB 将返回,而不处理列表中任何剩余的写操作。

无序操作

  • 通过无序操作列表,MongoDB 可以并行执行列表中的写入操作,也可以以不确定的顺序执行。如果在处理其中一个写操作的过程中发生错误,MongoDB将继续处理列表中剩余的写操作。
sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "city" : "sh", "age" : 66 }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : 26 }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

# 实例化构建器, 无序批量操作 !!!!!
sit_rs1:PRIMARY> var bulk = db.user.initializeUnorderedBulkOp();

# 只执行单文档替换操作
sit_rs1:PRIMARY> bulk.find( { name: "user8" } ).replaceOne( { name: "user8", status: "Modified", age: 22 } );

# 未执行 execute,数据还不会替换 !!!!!!!!!!!
sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "city" : "sh", "age" : 66 }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : 26 }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }


# 执行 Bulk.execute() 执行由操作构建器构建的操作列表Bulk() 
sit_rs1:PRIMARY> bulk.execute();
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 0,
        "nMatched" : 1,
        "nModified" : 1,
        "nRemoved" : 0,
        "upserted" : [ ]
})

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "Modified", "age" : 22 }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : 26 }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

Bulk.find.update(<update>) 将multi更新操作添加到批量操作列表。该方法更新现有文档中的特定字段。

使用该Bulk.find()方法指定确定要更新哪些文档的条件。Bulk.find.update() 方法更新所有匹配的文档。

以下示例为 user 集合的 初始化操作构建器 Bulk() ,并将 user8、user20 更新操作添加到操作列表中, 如下:

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "Modified", "age" : 22 }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : 26 }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

sit_rs1:PRIMARY> var bulk = db.user.initializeUnorderedBulkOp();

sit_rs1:PRIMARY> bulk.find( { name: "user8" } ).update( { $set: { status: "1", age: "33" } } );

sit_rs1:PRIMARY> bulk.find( { name: "user20" } ).update( { $set: { age: "55" } } );
 
sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "Modified", "age" : 22 }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : 26 }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

sit_rs1:PRIMARY> bulk.execute();
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 0,
        "nMatched" : 2,
        "nModified" : 2,
        "nRemoved" : 0,
        "upserted" : [ ]
})

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "33" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

Bulk.find.updateOne(<update>) 将单个文档更新操作添加到批量操作列表。

使用该Bulk.find()方法指定确定要更新哪个文档的条件。Bulk.find.updateOne() 方法将更新限制为单个文档。

以下示例为 user 集合的 初始化操作构建器 Bulk() ,并添加各种 updateOne() 操作到操作列表,如下:

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "33" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

sit_rs1:PRIMARY> var bulk = db.user.initializeUnorderedBulkOp();
 
sit_rs1:PRIMARY> bulk.find( { name: "user8" } ).update( { $set: { status: "1", age: "22" } } );

sit_rs1:PRIMARY> bulk.execute();
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 0,
        "nMatched" : 1,
        "nModified" : 1,
        "nRemoved" : 0,
        "upserted" : [ ]
})

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "22" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

Bulk.find.upsert() 将更新或替换操作的upsert选项设置为 true , 并具有以下语法:

Bulk.find(<query>).upsert().update(<update>);
Bulk.find(<query>).upsert().updateOne(<update>);
Bulk.find(<query>).upsert().replaceOne(<replacement>);

将upsert选项设置为 时true:

  • 如果不存在与Bulk.find()条件匹配的文档,则更新或替换操作将执行插入。
  • 如果匹配的文档确实存在,则更新或替换操作将执行指定的更新或替换。

使用 Bulk.find.upsert() 具有以下写入操作:

Bulk.find.replaceOne()

Bulk.find.updateOne()

Bulk.find.update()

以下示例, 使用upsert选项执行替换 name: “user12” 的用户,如果不存在,则插入新文档 。

如果用替换操作 Bulk.find.upsert() 选项执行插入,插入的文档是替换文档。如果替换文档和查询文档均未指定_id字段,MongoDB 会添加该_id字段:

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "22" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }

sit_rs1:PRIMARY> var bulk = db.user.initializeUnorderedBulkOp();

sit_rs1:PRIMARY> bulk.find( { name: "user12" } ).upsert().replaceOne(
...    {
...      name: "user12",
...      status: "0",
...      age: 12,
...    }
... );

sit_rs1:PRIMARY> bulk.execute();
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 1,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [
                {
                        "index" : 0,
                        "_id" : ObjectId("64df3619aed0456fbb0e919e")
                }
        ]
})

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "22" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }
{ "_id" : ObjectId("64df3619aed0456fbb0e919e"), "name" : "user12", "status" : "0", "age" : 12 }

以下示例,使用 updateOne 方法更新一条记录 { name: “user12” },如果不存在则执行插入。

如果更新操作为 Bulk.find.upsert() 选项执行插入,插入的文档是替换文档。如果替换文档和查询文档均未指定_id字段,MongoDB 会添加该_id字段。

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "22" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }
{ "_id" : ObjectId("64df3619aed0456fbb0e919e"), "name" : "user12", "status" : "0", "age" : 12 }

sit_rs1:PRIMARY> var bulk = db.user.initializeUnorderedBulkOp();

sit_rs1:PRIMARY> bulk.find( { name: "user12" } ).upsert().updateOne(
...    {
...      name: "user12",
...      status: "1",
...  age: 22,
...    }
... );

sit_rs1:PRIMARY> bulk.execute();
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 0,
        "nMatched" : 1,
        "nModified" : 1,
        "nRemoved" : 0,
        "upserted" : [ ]
})

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "22" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }
{ "_id" : ObjectId("64df3619aed0456fbb0e919e"), "name" : "user12", "status" : "1", "age" : 22 }

使用 upsert() 多文档更新方法 Bulk.find.update(),如果没有文档符合查询条件,则更新操作插入单个文档。

如果更新操作为 Bulk.find.upsert() 选项执行插入,更新操作插入单个文档,其中包含该方法的查询文档中的字段和值。文章来源地址https://www.toymoban.com/news/detail-659106.html

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "22" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }
{ "_id" : ObjectId("64df3619aed0456fbb0e919e"), "name" : "user12", "status" : "1", "age" : 22 }

sit_rs1:PRIMARY> var bulk = db.user.initializeUnorderedBulkOp();


sit_rs1:PRIMARY> bulk.find( { name: "user15" } ).upsert().update(
...    {
...      $setOnInsert: { "item" : { "A" : 111, "B" : 222 } },
...      $set: { status: "1", age: "50" }
...    }
... );

sit_rs1:PRIMARY> bulk.execute();
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 1,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [
                {
                        "index" : 0,
                        "_id" : ObjectId("64df389daed0456fbb0e9944")
                }
        ]
})

sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b8d2404b442dde59447cf4"), "name" : "user5", "age" : 57, "item" : { "A" : 112, "B" : 215 }, "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 99, "n2" : 4 }, { "n1" : 4, "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("64b8d2404b442dde59447cf5"), "name" : "chen", "status" : "Modified" }
{ "_id" : ObjectId("64b8d2404b442dde59447cf7"), "name" : "user8", "status" : "1", "age" : "22" }
{ "_id" : ObjectId("64db6285f386d37d5de1b830"), "name" : "user11", "age" : 50, "number" : 1 }
{ "_id" : 3, "name" : "user20", "city" : "sz", "age" : "55" }
{ "_id" : 4, "name" : "user21", "city" : "bj", "age" : 30 }
{ "_id" : ObjectId("64df3619aed0456fbb0e919e"), "name" : "user12", "status" : "1", "age" : 22 }
{ "_id" : ObjectId("64df389daed0456fbb0e9944"), "name" : "user15", "age" : "50", "item" : { "A" : 111, "B" : 222 }, "status" : "1" }

到了这里,关于Mongodb 更新集合的方法到底有几种 (下) ?的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • mongotop跟踪Mongodb集合读取和写入数据

    从 MongoDB 4.4 开始,MongoDB 数据库工具现在与 MongoDB 服务器分开发布,并使用自己的版本控制,初始版本为100.0.0. 此前,这些工具与 MongoDB 服务器一起发布,并使用匹配的版本控制。 mongotop 版本100.7.3支持以下版本的 MongoDB Server: MongoDB 6.0 MongoDB 5.0 MongoDB 4.4 MongoDB 4.2 行为 mongo

    2024年02月15日
    浏览(38)
  • MongoDB - 库、集合、文档(操作 + 演示 + 注意事项)

    目录 一、MongoDB 1.1、简介 a)MongoDB 是什么?为什么要使用 MongoDB? b)应用场景 c)MongoDB 这么强大,是不是可以直接代替 MySQL ? d)MongoDB 中的一些概念 e)Docker 下载 1.2、库操作 tips看前须知 a)查看所有库 b)创建并使用数据库 c)查看当前操作的数据库 d)删除数据库 1.3、

    2024年01月17日
    浏览(35)
  • Mongodb 集合插入文档自动生成ObjectId

    Mongodb 使用以下几种方法来插入文档 , Mongodb V5.0+ 使用 mongosh 客户端: 插入单个文档 db.collection.insertOne() 将单个 文档插入到集合中。 如果该集合当前不存在,则插入操作将创建该集合。 如果文档未指定_id字段,则将在插入之前 mongod 添加该字段并为文档_id分配唯一的字段

    2024年02月12日
    浏览(46)
  • 【MongoDB】数据库、集合、文档常用CRUD命令

    目录 一、数据库操作 1、创建数据库操作 2、查看当前有哪些数据库 3、查看当前在使用哪个数据库 4、删除数据库 二、集合操作 1、查看有哪些集合 2、删除集合 3、创建集合 三、文档基本操作 1、插入数据 2、查询数据 3、删除数据 4、修改数据 四、文档分页查询 五、文档其

    2024年02月13日
    浏览(43)
  • mongodb 数据库管理(数据库、集合、文档)

    目录 一、数据库操作 1、创建数据库 2、删除数据库 二、集合操作 1、创建集合 2、删除集合 三、文档操作 1、创建文档 2、 插入文档 3、查看文档 4、更新文档 1)update() 方法 2)replace() 方法 创建数据库的语法格式如下: 如果数据库不存在,则创建数据库,否则切换到该数据

    2024年02月12日
    浏览(49)
  • MongoDB常用的操作(服务器、数据库、集合)

    前面两篇已经介绍了MongoDB系统架构及其BSON数据类型。本文将讲解基本的MongoDB操作。 一、MongoDB启动命令 启动MongoDB 查看是否启动成功 重启MongoDB 关闭MongoDB 二、使用init命令操作MongoDB 启动MongoDB 查看是否启动成功 重启MongoDB 关闭MongoDB 三、启动mongodb客户端 查看该服务是否启动

    2024年02月07日
    浏览(61)
  • MongoDB系统性能调优(持续更新)

    cache_size 指定WT存储引擎内部cache的内存用量上限。 需要注意的是,仅作用于WiredTiger cache,而非mongod进程的内存用量上限。MongoDB同时使用WT cache和文件系统cache,往往mongod进程的内存用量高于该值。cache_size相对于物理内存总量不要设置的太满,需要留有一定内存为操作系统所

    2024年01月19日
    浏览(32)
  • MongoDB 更新文档(更新数组对象中的元素)

    之前我们介绍了如何更新文档,并简单的介绍了更新文档时可以使用选项进行指定当更新内容不存在时,可以进行新增文档。具体可以参考: MongoDB 更新文档(更新一条文档) https://blog.csdn.net/m1729339749/article/details/129983304 最近遇到了一个需求,文档中包含了一个数组对象, 需要

    2024年02月13日
    浏览(41)
  • 在Node.js中使用MongoDB连接数据库、创建集合

    本文主要介绍在Node.js中使用MongoDB连接数据库、创建集合的方法。 在Node.js中使用MongoDB连接数据库有两种方式:使用原生驱动程序和使用Mongoose。 首先,需要安装 mongodb 模块。可以通过以下命令来安装: 安装完成后,可以在代码中引入模块: 接着,可以使用 MongoClient 来连接

    2024年02月04日
    浏览(49)
  • MongoDB原生语句更新嵌套数组的值

    首先执行MongoDB原生语句脚本在user集合中产生一些样本数据,如下所示: 如果想将 “ 吕子乔 ” 修改为 “ 曾小贤 ” ,则可以执行下述脚本进行数据更新: 这时再次查看user集合,可以看到数据已经更新: 此处使用了$符号,$符号表示的是一个占位符,代表被匹配的数组中的

    2024年02月15日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包