《MongoDB入门教程》第14篇 CRUD之更新文档
本篇我們將會介紹如何使用集合的 updateOne() 和 updateMany() 方法更新文檔。
updateOne() 方法
updateOne() 方法可以更新滿足條件的單個文檔,語法如下:
db.collection.updateOne(filter, update, options)其中,
- filter 用于指定一個查詢條件。如果多個文檔都滿足條件,updateOne() 只會更新第一個滿足條件的文檔。如果指定一個空文檔({})作為查詢條件,updateOne() 將會更新集合中返回的第一個文檔。
- update 用于指定更新操作。
- options 參數用于指定一些更新選項,本文不涉及相關內容。
updateOne() 方法將會返回一個結果文檔,包括但不限于以下字段:
- matchedCount 字段返回了滿足條件的文檔數量。
- modifiedCount 字段返回了被更新的文檔數量。對于 updateOne() 方法,結果為 0 或者 1。
$set 操作符
$set 操作符用于替換指定字段的值,語法如下:
{ $set: { <field1>: <value1>, <field2>: <value2>, ...}}如果指定的字段不存在,$set 操作符將會為文檔增加一個新的字段,只要新的字段不違法類型約束。
如果指定字段時使用了點符合,例如 embededDoc.field,同時字段 field 不存在,$set 操作符將會創建一個嵌入式文檔。
updateOne() 示例
接下來的示例將會使用以下 products 集合:
db.products.insertMany([{ "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]},{ "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]},{ "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]},{ "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]},{ "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}])示例一:更新單個文檔
以下示例使用 updateOne() 方法更新 _id 等于 1 的文檔中的 price 字段:
db.products.updateOne({_id: 1 }, {$set: {price: 899} })其中,
- { _id : 1 } 是查找文檔的條件。
- { $set: { price: 899 } } 指定了更新操作,利用 $set 操作符將 price 字段修改為 899。
示例返回的結果如下:
{acknowledged: true,insertedId: null,matchedCount: 1,modifiedCount: 1,upsertedCount: 0 }在返回結果中,matchedCount 代表了滿足查詢條件的文檔數量(1),modifiedCount 代表了被修改的文檔數量(1)。
我們可以使用 findOne() 方法驗證修改后的文檔內容:
db.products.findOne({ _id: 1 }, { name: 1, price: 1 }){ _id: 1, name: 'xPhone', price: 899 }示例二:更新第一個匹配的文檔
以下查詢返回了價格等于 899 的所有產品:
db.products.find({ price: 899 }, { name: 1, price: 1 })[{ _id: 1, name: 'xPhone', price: 899 },{ _id: 2, name: 'xTablet', price: 899 },{ _id: 3, name: 'SmartTablet', price: 899 } ]以下示例使用 updateOne() 方法更新價格等于 899 的第一個文檔:
db.products.updateOne({ price: 899 }, { $set: { price: null } }){acknowledged: true,insertedId: null,matchedCount: 1,modifiedCount: 1,upsertedCount: 0 }按照默認的返回順序,第一個價格等于 899 的產品 _id 等于 1,它的價格被更新為 null:
db.products.find({ _id: 1}, { name: 1, price: 1 })[ { _id: 1, name: 'xPhone', price: null } ]示例三:更新嵌入式文檔
以下查詢返回了 _id 等于 4 的文檔:
db.products.find({ _id: 4 }, { name: 1, spec: 1 })[{_id: 4,name: 'SmartPad',spec: { ram: 8, screen: 9.7, cpu: 1.66 }} ]下面的示例使用 updateOne() 方法更新該文檔中的嵌入式文檔 spec 的 ram、screen 以及 cpu 字段:
db.products.updateOne({_id: 4 }, {$set: {"spec.ram": 16,"spec.screen": 10.7,"spec.cpu": 2.66} })操作返回的結果如下:
{acknowledged: true,insertedId: null,matchedCount: 1,modifiedCount: 1,upsertedCount: 0 }再次查詢文檔中的內容:
db.products.find({ _id: 4 }, { name: 1, spec: 1 })[{_id: 4,name: 'SmartPad',spec: { ram: 16, screen: 10.7, cpu: 2.66 }} ]示例四:更新數組元素
以下示例使用 updateOne() 方法更新文檔(_id: 4)中數組字段 storage 的第一個和第二個元素:
db.products.updateOne({ _id: 4}, {$set: {"storage.0": 16,"storage.1": 32}} )返回結果如下:
{acknowledged: true,insertedId: null,matchedCount: 1,modifiedCount: 1,upsertedCount: 0 }再次查詢該文檔中的內容:
db.products.find({ _id: 4 }, { name: 1, storage: 1 });[ { _id: 4, name: 'SmartPad', storage: [ 16, 32, 1024 ] } ]updateMany() 方法
updateMany() 方法可以更新滿足條件的所有文檔,語法如下:
db.collection.updateMany(filter, update, options)其中,
- filter 用于指定一個查詢條件。如果指定一個空文檔({})作為查詢條件,將會更新集合中的全部文檔。
- update 用于指定更新操作。
- options 參數用于指定一些更新選項,本文不涉及相關內容。
updateMany() 方法將會返回一個結果文檔,包括但不限于以下字段:
- matchedCount 字段返回了滿足條件的文檔數量。
- modifiedCount 字段返回了被更新的文檔數量。
updateMany() 同樣使用 $set 操作符執行更新操作。
updateMany() 示例
示例一:更新多個文檔
以下示例使用 updateMany() 方法更新價格為 899 的所有產品:
db.products.updateMany({ price: 899}, { $set: { price: 895 }} )其中,
- { price : 899 } 指定了查找文檔的條件。
- { $set: { price: 895} } 指定了更新操作,利用 $set 操作符將 price 字段修改為 895。
查詢返回的結果如下:
{acknowledged: true,insertedId: null,matchedCount: 2,modifiedCount: 2,upsertedCount: 0 }在返回結果中,matchedCount 代表了滿足查詢條件的文檔數量(2),modifiedCount 代表了被更新的文檔數量(2)。
再次通過價格(895)查詢產品:
db.products.find({price: 895 }, {name: 1,price: 1 })查詢返回的結果如下:
[{ _id: 2, name: 'xTablet', price: 895 },{ _id: 3, name: 'SmartTablet', price: 895 } ]示例二:更新嵌入式文檔
以下示例使用 updateMany() 更新價格大于 700 的產品中嵌入式文檔 spec 的 ram、screen 以及 cpu 字段:
db.products.updateMany({price: { $gt: 700} }, {$set: {"spec.ram": 32,"spec.screen": 9.8,"spec.cpu": 5.66} })返回結果顯示成功更新 3 個文檔:
{acknowledged: true,insertedId: null,matchedCount: 3,modifiedCount: 3,upsertedCount: 0 }示例三:更新數組元素
以下示例使用 updateMany() 方法更新文檔(_id 等于 1、2、3)中的數組字段 storage 的第一個元素和第二個元素:
db.products.updateMany({_id: {$in: [1, 2, 3]} }, {$set: {"storage.0": 16,"storage.1": 32} })返回結果如下:
{acknowledged: true,insertedId: null,matchedCount: 3,modifiedCount: 3,upsertedCount: 0 }總結
以上是生活随笔為你收集整理的《MongoDB入门教程》第14篇 CRUD之更新文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 震惊!图书编辑公开怼了500条读者评论~
- 下一篇: 【影片制作】分镜制作方法