MongoDB提供以下方法来更新集合中的文档:
- update:更新或替换匹配指定过滤器的单个文档,或更新与指定过滤器匹配的所有文档,要更新多个文档,请使用multi选项。
- updateOne:更新单个文档记录
- updateMany:更新多个文档记录(修改所有匹配到的文档记录)
- replaceOne:将文档替换为新的文档
- update更新单条数据
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "amount" : 500, "status" : "A" }
{ "_id" : 1, "cust_id" : "A123", "amount" : 450, "status" : "A" }
{ "_id" : 2, "cust_id" : "B130", "amount" : 500, "status" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 300, "status" : "B" }
rs02:PRIMARY> db.orders.update({"_id" : 1},{$set:{"amount" : 250}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "amount" : 500, "status" : "A" }
{ "_id" : 1, "cust_id" : "A123", "amount" : 250, "status" : "A" } //_id:1 数据已被修改
{ "_id" : 2, "cust_id" : "B130", "amount" : 500, "status" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 300, "status" : "B" }
- updateOne更新单条数据
rs02:PRIMARY> db.orders.updateOne({"status" : "A"},{$set:{ "amount" : 100}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "amount" : 100, "status" : "A" }
{ "_id" : 1, "cust_id" : "A123", "amount" : 250, "status" : "A" }
{ "_id" : 2, "cust_id" : "B130", "amount" : 500, "status" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 300, "status" : "B" }
- updateMany更新多个文档
rs02:PRIMARY> db.orders.updateMany({"status" : "A"},{$set:{ "amount" : 200}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "amount" : 200, "status" : "A" }
{ "_id" : 1, "cust_id" : "A123", "amount" : 200, "status" : "A" }
{ "_id" : 2, "cust_id" : "B130", "amount" : 200, "status" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 300, "status" : "B" }
- 修改一条数据多个字段的值
rs02:PRIMARY> db.orders.update({"_id" : 2},{$set:{"cust_id":"B212","amount" : 200}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "amount" : 500, "status" : "A" }
{ "_id" : 1, "cust_id" : "A123", "amount" : 250, "status" : "A" }
{ "_id" : 2, "cust_id" : "B212", "amount" : 200, "status" : "A" } //_id:2的数据已被修改
{ "_id" : 3, "cust_id" : "A123", "amount" : 300, "status" : "B" }
- 使用参数$unset 删除集合字段
rs02:PRIMARY> db.orders.updateMany({"status" : "A"},{$unset:{ "amount" : ""}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
rs02:PRIMARY> db.orders.find() //删除了"amount" 字段
{ "_id" : 0, "cust_id" : "A123", "status" : "A" }
{ "_id" : 1, "cust_id" : "A123", "status" : "A" }
{ "_id" : 2, "cust_id" : "B212", "status" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 300, "status" : "D" }
- 使用参数$rename重命名字段名称
rs02:PRIMARY> db.orders.updateMany({},{$rename:{"status":"status_1"}})
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 4 } //matchedCount表示匹配到的文档记录数
rs02:PRIMARY> db.orders.find() //modifiedCount表示修改的文档记录数
{ "_id" : 0, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 1, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 2, "cust_id" : "B212", "status_1" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 300, "status_1" : "D" }
rs02:PRIMARY>
- 使用参数$min和要修改的字段值进行对比,如果指定的值小于原值进行修改,如果指定值大于原来值不修改
rs02:PRIMARY> db.orders.update({"_id":3},{$min:{"amount":200}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 1, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 2, "cust_id" : "B212", "status_1" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 200, "status_1" : "D" } //amount值更新为200
rs02:PRIMARY> db.orders.update({"_id":3},{$min:{"amount":300}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 1, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 2, "cust_id" : "B212", "status_1" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 200, "status_1" : "D" } //amount值不更新
- 使用参数$max和要修改的字段值进行对比,如果指定的值大于原值进行修改,如果指定值小于原来值不修改
rs02:PRIMARY> db.orders.update({"_id":3},{$max:{"amount":399}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 1, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 2, "cust_id" : "B212", "status_1" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 399, "status_1" : "D" } //amount值修改为399
rs02:PRIMARY> db.orders.update({"_id":3},{$max:{"amount":200}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 1, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 2, "cust_id" : "B212", "status_1" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 399, "status_1" : "D" } //amount值不修改
- 使用upsert: true,如果updateOne()、updateMany()或replaceOne()包含upsert: true且没有文档匹配指定的筛选器,则该操作将创建一个新文档并插入它。如果存在匹配的文档,则该操作修改或替换匹配的文档。
rs02:PRIMARY> db.orders.update({"status" : "A"},{$set:{ "amount" : 111}},{ upsert: true })
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("637dc22ca8556607927da3f3")
})
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 1, "cust_id" : "A123", "status_1" : "A" }
{ "_id" : 2, "cust_id" : "B212", "status_1" : "A" }
{ "_id" : 3, "cust_id" : "A123", "amount" : 399, "status_1" : "D" }
{ "_id" : ObjectId("637dc22ca8556607927da3f3"), "status" : "A", "amount" : 111 } //没有匹配到则插入文档
rs02:PRIMARY>
- update使用multi:true参数
rs02:PRIMARY> db.orders.update({"status_1" : "A"},{$set:{ "amount" : 200}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 }) //匹配到三条数据,更新三条数据
rs02:PRIMARY> db.orders.find()
{ "_id" : 0, "cust_id" : "A123", "status_1" : "A", "amount" : 200 }
{ "_id" : 1, "cust_id" : "A123", "status_1" : "A", "amount" : 200 }
{ "_id" : 2, "cust_id" : "B212", "status_1" : "A", "amount" : 200 }
{ "_id" : 3, "cust_id" : "A123", "amount" : 399, "status_1" : "D" }
{ "_id" : ObjectId("637dc22ca8556607927da3f3"), "status" : "A", "amount" : 111 }
rs02:PRIMARY>
- updateOne使用$currentDate参数
使用$set操作符更新size的值。将status字段的值设置为“P”,使用$currentDate操作符将lastModified字段的值更新为当前日期。如果lastModified字段不存在,$currentDate将创建该字段。
rs02:PRIMARY> db.inventory.find()
{ "_id" : ObjectId("637b2e69cb0613dfb280e899"), "item" : "canvas", "qty" : 100, "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89a"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89b"), "item" : "mat", "qty" : 85, "size" : { "h" : 27.9, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89c"), "item" : "mousepad", "qty" : 25, "size" : { "h" : 19, "w" : 22.85, "uom" : "cm" }, "status" : "P" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89d"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "P" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89e"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89f"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a0"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a1"), "item" : "sketchbook", "qty" : 80, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a2"), "item" : "sketch pad", "qty" : 95, "size" : { "h" : 22.85, "w" : 30.5, "uom" : "cm" }, "status" : "A" }
rs02:PRIMARY> db.inventory.updateOne(
... { item: "paper" },
... {
... $set: { "size.uom": "cm", status: "P" },
... $currentDate: { lastModified: true }
... }
... )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } //匹配并更新一条数据,新建日期字段
rs02:PRIMARY> db.inventory.find()
{ "_id" : ObjectId("637b2e69cb0613dfb280e899"), "item" : "canvas", "qty" : 100, "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89a"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89b"), "item" : "mat", "qty" : 85, "size" : { "h" : 27.9, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89c"), "item" : "mousepad", "qty" : 25, "size" : { "h" : 19, "w" : 22.85, "uom" : "cm" }, "status" : "P" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89d"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "P" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89e"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "cm" }, "status" : "P", "lastModified" : ISODate("2022-11-21T08:08:04.570Z") }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89f"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a0"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a1"), "item" : "sketchbook", "qty" : 80, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a2"), "item" : "sketch pad", "qty" : 95, "size" : { "h" : 22.85, "w" : 30.5, "uom" : "cm" }, "status" : "A" }
rs02:PRIMARY>
- updateMany使用$currentDate参数
rs02:PRIMARY> db.inventory.updateMany(
... { "qty": { $lt: 50 } },
... {
... $set: { "size.uom": "in", status: "P" },
... $currentDate: { lastModified: true }
... }
... )
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 } //匹配并更新三条数据,新建日期字段
rs02:PRIMARY> db.inventory.find()
{ "_id" : ObjectId("637b2e69cb0613dfb280e899"), "item" : "canvas", "qty" : 100, "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89a"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2022-11-21T08:09:26.903Z") }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89b"), "item" : "mat", "qty" : 85, "size" : { "h" : 27.9, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89c"), "item" : "mousepad", "qty" : 25, "size" : { "h" : 19, "w" : 22.85, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2022-11-21T08:09:26.904Z") }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89d"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "P" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89e"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "cm" }, "status" : "P", "lastModified" : ISODate("2022-11-21T08:08:04.570Z") }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89f"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a0"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2022-11-21T08:09:26.904Z") }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a1"), "item" : "sketchbook", "qty" : 80, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a2"), "item" : "sketch pad", "qty" : 95, "size" : { "h" : 22.85, "w" : 30.5, "uom" : "cm" }, "status" : "A" }
rs02:PRIMARY>
- 使用replaceOne替换文档
要替换文档中除id字段外的全部内容,将一个全新的文档作为第二个参数传递给db.collection.replaceOne()。替换文档时,替换文档必须仅由字段/价值对组成;即不包括更新操作员的表达。替换文档可以具有与原始文档不同的字段。在替换文档中,可以省略id字段,因为id字段是不可变的;但是,如果包含id字段,则它必须具有与当前值相同的值。
rs02:PRIMARY> db.inventory.replaceOne(
... { item: "paper" },
... { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
... )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } //匹配并替换一个文档
rs02:PRIMARY> db.inventory.find()
{ "_id" : ObjectId("637b2e69cb0613dfb280e899"), "item" : "canvas", "qty" : 100, "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89a"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2022-11-21T08:09:26.903Z") }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89b"), "item" : "mat", "qty" : 85, "size" : { "h" : 27.9, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89c"), "item" : "mousepad", "qty" : 25, "size" : { "h" : 19, "w" : 22.85, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2022-11-21T08:09:26.904Z") }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89d"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "P" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89e"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 40 } ] }
{ "_id" : ObjectId("637b2e69cb0613dfb280e89f"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a0"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2022-11-21T08:09:26.904Z") }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a1"), "item" : "sketchbook", "qty" : 80, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("637b2e69cb0613dfb280e8a2"), "item" : "sketch pad", "qty" : 95, "size" : { "h" : 22.85, "w" : 30.5, "uom" : "cm" }, "status" : "A" }
rs02:PRIMARY>
一个$replaceRoot阶段,使用$mergeObjects表达式为quiz1、quiz2、test1和test2字段设置默认值。聚合变量ROOT引用被修改的当前文档。要访问该变量,请以$$作为前缀并用引号括起来。当前文档字段将覆盖默认值。$set阶段用于将修改的字段更新为当前日期时间。该操作对当前日期时间使用聚合变量NOW。要访问该变量,请以$$作为前缀并用引号括起来。
rs02:PRIMARY> db.students2.find()
{ "_id" : 1, "quiz1" : 8, "test2" : 100, "quiz2" : 9, "modified" : ISODate("2020-01-04T16:00:00Z") }
{ "_id" : 2, "quiz2" : 5, "test1" : 80, "test2" : 89, "modified" : ISODate("2020-01-04T16:00:00Z") }
rs02:PRIMARY> db.students2.updateMany( {},
... [
... { $replaceRoot: { newRoot:
... { $mergeObjects: [ { quiz1: 0, quiz2: 0, test1: 0, test2: 0 }, "$$ROOT" ] }
... } },
... { $set: { modified: "$$NOW"} }
... ]
... )
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
rs02:PRIMARY> db.students2.find()
{ "_id" : 1, "quiz1" : 8, "quiz2" : 9, "test1" : 0, "test2" : 100, "modified" : ISODate("2022-11-21T09:31:18.154Z") }
{ "_id" : 2, "quiz1" : 0, "quiz2" : 5, "test1" : 80, "test2" : 89, "modified" : ISODate("2022-11-21T09:31:18.154Z") }
rs02:PRIMARY>