252_Mongodb_增刪改查_改update
update 更新

db.collection.updateOne()
db.collection.updateMany()
db.collection.replaceOne()
db.collection.save()
語法
db.collection.update(
更新操作符
操作符類型
運算符
描述
更新操作
$inc
指定值加n
$set
指定值修改為n
$unset
將指定字段刪除
$rename
更新字段名稱
$setOnInsert
更新導致新增文檔,則設置字段的值。文檔存在不影響
$currentDate
指定字段賦值為當前時間值
$min? $max $mul
指定值小于現有字段值時才更新字段
指定值大于現有字段值時才更新字段
將字段的值乘以指定的金額
數組操作
$
定位到某一個元素
$push
添加值到數組中
$addToSet
添加值到數組中,有重復則不處理
$pop
刪除數組第一個或者最后一個
$pull
$pullAll
從數組中刪除匹配查詢條件的值
從數組中刪除多個值
數組運算修飾
$each
每一個
$slice
分割
$sort
往數組添加元素的排序
位更新
$bit
執行整數值的按位AND、OR和XOR更新
/ / 更新單個文檔,會更新第一個匹配到的文檔 // UPDATE inventory SET size.uom='cm', status='P', lastModified=now() WHERE item = "paper" db.inventory.updateOne( { item: "paper" }, { $set: { "size.uom": "cm", status: "P" }, $currentDate: { lastModified: true } } ) // 批量更新 // UPDATE inventory SET size.uom='in', status='P', lastModified=now() WHERE qty < 50 db.inventory.updateMany( { "qty": { $lt: 50 } }, { $set: { "size.uom": "in", status: "P" }, $currentDate: { lastModified: true } } ) db.collection.save() # save 更新文檔必須加上 “_id” 通過 _id字段對原文檔進行覆蓋更新 db.collection.replaceOne() # replaceOne() 不需要使用”_id” 字段,并且只更新匹配到的第一個數據;但是先刪除后添加操作
// 替換 db.inventory.replaceOne( { item: "paper" }, { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] } ) // 刪除字段 db.inventory.find({"item":"map"}) db.inventory.updateOne({"item":"map"},{"$unset":{"status":""}}) // 更新字段名 db.inventory.updateMany({"item":"map"},{"$rename":{"lastModified":"updateTime"}})
// 數組操作 db.inventory.find({"item":"mobile"}) // 往tags數組中增加元素,如果tags字段不存在則會新增該字段,如果值有重復就不處理 db.inventory.updateMany({"item":"mobile"},{$addToSet:{tags:"c"}}) // 數組中添加數組元素,并不是添加多個值 db.inventory.updateMany({"item":"mobile"},{$addToSet:{tags:["o","p"]}}) # 數組中添加多個元素 db.inventory.updateMany({"item":"mobile"}, {$push: {tags: {$each:["aa","bb"]}}}) // 通過$each,往數組中添加多個值 db.inventory.update( { item: "mobile" }, { $push: { scores: { $each: [ 90, 92, 85, 88, 100 ] } } } )
// 往數組添加三個元素,但是只保留最后三個 db.inventory.update( {item:"mobile"},{$push: {scores: {$each: [ 80,78,86], $slice: -4}} }) // 更新所有記錄,刪除tags數組的red、big元素 db.inventory.find({tags: {$in: ["red", "big"] } }) db.inventory.update( { },{ $pull: { tags: { $in: [ "red", "big" ] }} },{ multi: true } ) // 刪除length數組中包含50/90的元素 db.inventory.update( { item: "apple" }, { $pullAll: { length: [ 150, 390 ] } } ) // 從前彈出一個元素,類似隊列/棧 的pop api db.inventory.find( { item:"mobile"} ) db.inventory.update( { item:"mobile"}, { $pop: { scores: -1 } } ) // 從后彈出1個元素 db.inventory.update( { item:"mobile"}, { $pop: { scores: 1 } } ) // 數組元素過濾 // 將length數組中大于100的元素,調整為100 db.inventory.find( { length: {$gte:100} } ) db.inventory.update( {"item" : "map"}, { $set: { "length.$[element]" : 100 } }, { multi: true,arrayFilters: [ { "element": { $gte: 100 } } ]} ) db.inventory.insertOne({ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }) db.inventory.update({item: "canvas"}, {$set: {qty: 1000}}) db.inventory.insertOne({ item: "canvas2", qty: 100, tags: ["cotton","cotton111"], size: { h: 28, w: 35.5, uom: "cm" } }) db.inventory.update({item: "canvas2"}, {$set: {"tags.$[i]":"glass3"}}, {arrayFilters: [{"i":{$eq:"glass2"}}]});
MongoDB 數據結構
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。