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() 选项执行插入,更新操作插入单个文档,其中包含该方法的查询文档中的字段和值。

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" }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/50181.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java8 实现批量插入和更新,SpringBoot实现批量插入和更新,Mybatis实现批量插入和更新

前言 基于mybatis实现的批量插入和更新 由于直接执行批量所有数据可能会出现长度超出报错问题&#xff0c;使用如下方式即可解决 实现 原理还是分配执行&#xff0c;这里的100就是设定每次执行最大数 /*** 封装使用批量添加或修改数据库操作* * param list 集合* param inse…

dnsmasq-dhcp DHCPDISCOVER “no address available“ 问题解决方法

问题现象 在Centos7.5系统中已安装dnsmasq组件并开启DHCP服务功能&#xff0c;然而客户端无法通过DHCP的方式获取IP&#xff0c;通过查看系统日志/var/log/messages发现日志中存在以下两个关键信息&#xff1a; dnsmasq-dhcp DHCPDISCOVER “no address available”DHCPNAK 1…

什么是神经网络

什么是神经网络 什么是神经网络&#xff1f;CNN、RNN、GNN&#xff0c;这么多的神经网络&#xff0c;有什么区别和联系&#xff1f; 既然我们的目标是打造人工智能&#xff0c;拥有智慧的大脑无疑是最好的模仿对象&#xff0c;人脑中有约860亿个神经元&#xff0c;这被认为是…

量化开发学习入门-概念篇

1.网格交易法 网格交易法&#xff08;Grid Trading&#xff09;是一种基于价格波动和区间震荡的交易策略。它适用于市场处于横盘或震荡的情况下。 网格交易法的基本思想是在设定的价格区间内均匀地建立多个买入和卖出水平&#xff08;网格&#xff09;&#xff0c;并在价格上…

设计模式-责任链

在现代的软件开发中&#xff0c;程序低耦合、高复用、w易拓展、易维护 什么是责任链 责任链模式是一种行为设计模式&#xff0c; 允许你将请求沿着处理者链进行发送。收到请求后&#xff0c; 每个处理者均可对请求进行处理&#xff0c; 或将其传递给链上的下个处理者。 使用场景…

ModaHub魔搭社区:WinPlan企业经营垂直大模型数据建模(二)

目录 维度模版管理 录入维度数据 经营指标 创建经营指标 经营指标管理 维度模版管理 创建维度后,可在维度库的左侧栏展示全部启用中的维度,你也可以再次编辑维度模版;如不再需要该维度,可停用,停用后可在停用管理里重新启用或删除。 1)停用:维度停用后,不会出现在…

LAMP架构

这里写目录标题 LAMP架构一.LAMP架构的组成二.CGI和fastcgi1.CGI2.fastcgi3.比较4.PHP4.2**的** **Opcode** **语言**4.3PHP 配置 三.编译安装Apache http服务1.环境准备2.安装环境依赖包3.解压软件包4.移动apr包 apr-util包到安装目录中&#xff0c;并切换到 httpd-2.4.29目录…

拓扑排序Topological sorting/DFS C++应用例题P1113 杂务

拓扑排序 拓扑排序可以对DFS的基础上做变更从而达到想要的排序效果。因此&#xff0c;我们需要xy准备&#xff0c;vis数组记录访问状态&#xff0c;每一个任务都可以在dfs的过程中完成。 在使用拓扑排序方法时一些规定&#xff1a; 通常使用一个零时栈不会直接输出排序的节点…

压缩包安装mysql

删除 MySQL sc delete mysql 安装mysql mysqld -install

EasyExcel实现多sheet文件导出

文章目录 EasyExcel引入依赖表结构学生表课程表教师表 项目结构下载模板实体类StudentVoCourseVoTeacherVo ControllerServiceEasyExcelServiceStudentServiceCourseServiceTeacherService ServiceImplEasyExcelServiceImplStudentServiceImplCourseServiceImplTeacherServiceI…

Ubuntu 配置国内源

配置国内源 因为众所周知的原因&#xff0c;国外的很多网站在国内是访问不了或者访问极慢的&#xff0c;这其中就包括了Ubuntu的官方源。 所以&#xff0c;想要流畅的使用apt安装应用&#xff0c;就需要配置国内源的镜像。 市面上Ubuntu的国内镜像源非常多&#xff0c;比较有…

cuda编程day001

一、环境&#xff1a; ①、linux cuda-11.3 opecv4.8.0 不知道头文件和库文件路径&#xff0c;用命令查找&#xff1a; # find /usr/local -name cuda.h 2>/dev/null # 查询cuda头文件路径 /usr/local/cuda-11.3/targets/x86_64-linux/include/cuda.h # find /usr/…

走进图算法:C语言实现图的表示与深度优先搜索

走进图算法&#xff1a;C语言实现图的表示与深度优先搜索 图是一种重要的数据结构&#xff0c;它在计算机科学中广泛用于表示各种关系和网络。本篇博客将深入介绍图的基本概念、邻接矩阵表示方法以及深度优先搜索&#xff08;DFS&#xff09;算法的C语言实现示例。 图的基本概…

C#的索引器

索引器 在 C# 中&#xff0c;索引器&#xff08;Indexer&#xff09;是一种特殊的属性&#xff0c;允许通过类的实例像访问数组一样访问对象的元素。索引器允许将类的实例作为集合来使用&#xff0c;可以按照自定义的方式访问类中的元素。 索引器的定义类似于属性&#xff0c…

wazuh环境配置及漏洞复现

目录 一、wazuh配置 1进入官网下载OVA启动软件 2.虚拟机OVA安装 二、wazuh案例复现 1.wazuh初体验 2.这里我们以SQL注入为例&#xff0c;在我们的代理服务器上进行SQL注入&#xff0c;看wazuh如何检测和响应 一、wazuh配置 1进入官网下载OVA启动软件 Virtual Machine (O…

Springboot使用Hibernate-search插件实现搜索

Hibernate search入门 原理是运用hibernate结合lucene来实现局部索引 1、引入jar包配置&#xff08;Hibernate和Lucene包&#xff09; <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-search-orm</artifactId> <…

2023国赛数学建模思路 - 案例:粒子群算法

文章目录 1 什么是粒子群算法&#xff1f;2 举个例子3 还是一个例子算法流程算法实现建模资料 # 0 赛题思路 &#xff08;赛题出来以后第一时间在CSDN分享&#xff09; https://blog.csdn.net/dc_sinor?typeblog 1 什么是粒子群算法&#xff1f; 粒子群算法&#xff08;Pa…

c# 方法参数修饰符(out、ref、in)的区别

在C#中&#xff0c;ref、out和in是三种方法参数修饰符&#xff0c;它们在传递参数的方式和作用上有所不同。 ref修饰符&#xff1a; 传递方式&#xff1a;使用ref修饰符的参数可以是输入输出参数&#xff0c;即在方法调用前后都可以对其进行修改。 作用&#xff1a;通过ref修…

js实现滚轮滑动到底部自动加载(完整版)

这里我们用vue实现(原生js相似), 这里我们用一个div当作一个容器; <div class="JL" @scroll="onScroll" ref="inin"> <div v-for="(item,index) in this.list" :key="index" > ....…

JavaScript:交集和差集的应用场景

在集合A和集合B中&#xff0c;属于集合A&#xff0c;同时也属于集合B的元素组成的集合&#xff0c;就是交集。 在A中所有不属于集合B元素&#xff0c;组合成集合&#xff0c;就是差集。 那么在平时的开发中&#xff0c;如何使用差集和交集来解决问题呢&#xff1f; 现在有这…