252_Mongodb_增删改查_改update
【摘要】 update 更新操作
update 更新
- db.collection.updateOne()
- db.collection.updateMany()
- db.collection.replaceOne()
- db.collection.save()
语法
db.collection.update(
<query> ,
<update> ,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument 1>, … ],
hint: // MongoDB 4.2中支持的功能 } )
参数说明
query,预更新文档的查询条件,类似sql update查询的where
update, 更新的字段和字段值, update的对象和一些更新的操作符(如$inc $set)等,类似sql update查询的set
--upsert,可选,默认false, 表示如果记录不存在,是否插新文 档,true为插入,false为不插入
--multi,可选, 默认false, 表更新找到的第一条记录, 若设置true 表匹配的多条记录全部更新
writeConcern,可选,写策略
collation,可选,用来指定更新的排序规则
arrayFilters,可选,过滤文档数组,用于确定更 新操作要修改数组中的具体元素
hint,可选的,指定查询希望使用的索引字段,如果没有这个索引就会报错
更新操作符
操作符类型 |
运算符 |
描述 |
更新操作 |
$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"}}]});
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)