更新方法
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(),如果没有文档符合查询条件,则更新操作插入单个文档。文章来源:https://www.toymoban.com/news/detail-659106.html
如果更新操作为 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模板网!