博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongo数据更新(修改器)
阅读量:6224 次
发布时间:2019-06-21

本文共 6443 字,大约阅读时间需要 21 分钟。

数据更新

简单的做法是删除重新插入
update()函数
语法
db.集合.update(更新条件,新的对象数据(更新操作符),upsert,multi)
upsert如果要更新的数据不存在,则增加一条新的内容(true为增加,false不增加)
multi:是否只更新满足条件的第一条记录,设置为false只更行第一个,true全更新

> db.stu.find({},{
"_id":0}){ "name" : "张三", "sex" : "男", "age" : 18, "score" : 70, "address" : "河南" }{ "name" : "李四", "sex" : "女", "age" : 20, "score" : 60, "address" : "山东" }{ "name" : "王五", "sex" : "男", "age" : 17, "score" : 44, "address" : "江苏" }{ "name" : "赵六", "sex" : "男", "age" : 21, "score" : 80, "address" : "山东" }{ "name" : "孙七", "sex" : "女", "age" : 23, "score" : 50, "address" : "湖北" }{ "name" : "tom", "sex" : "男", "age" : 24, "score" : 20, "address" : "海南" }{ "name" : "lucy", "sex" : "女", "age" : 21, "score" : 62, "address" : "浙江" }{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国" }{ "name" : "smith", "sex" : "男", "age" : 19, "score" : 88, "address" : "美国" }#1.把年龄<20岁的分数更新为60db.stu.find({
"age":{
"$lt":20}},{
"_id":0});{ "name" : "张三", "sex" : "男", "age" : 18, "score" : 70, "address" : "河南" }{ "name" : "王五", "sex" : "男", "age" : 17, "score" : 44, "address" : "江苏" }{ "name" : "smith", "sex" : "男", "age" : 19, "score" : 88, "address" : "美国" }db.stu.update({
"age":{
"$lt":20}},{
"$set":{
"score":100}},false,false);WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })#只修改了一个db.stu.find({
"age":{
"$lt":20}},{
"_id":0});{ "name" : "张三", "sex" : "男", "age" : 18, "score" : 100, "address" : "河南" }{ "name" : "王五", "sex" : "男", "age" : 17, "score" : 44, "address" : "江苏" }{ "name" : "smith", "sex" : "男", "age" : 19, "score" : 88, "address" : "美国" }#全部修改db.stu.update({
"age":{
"$lt":20}},{
"$set":{
"score":100}},false,true);db.stu.find({
"age":{
"$lt":20}},{
"_id":0});{ "name" : "张三", "sex" : "男", "age" : 18, "score" : 100, "address" : "河南" }{ "name" : "王五", "sex" : "男", "age" : 17, "score" : 100, "address" : "江苏" }{ "name" : "smith", "sex" : "男", "age" : 19, "score" : 100, "address" : "美国" }save()db.stu.save({
"_id" : ObjectId("592cc2e00be561e9e879bcb5"),"name":'张三三'});只能根据_id更新

修改器

1.$inc :针对于一个数字字段,修改数字字段内容

语法: {"$inc":{"字段":步调}}

db.stu.find({},{
"_id":0}); { "name" : "张三", "sex" : "男", "age" : 18, "score" : 70, "address" : "河南" }{ "name" : "李四", "sex" : "女", "age" : 20, "score" : 60, "address" : "山东" }{ "name" : "王五", "sex" : "男", "age" : 17, "score" : 44, "address" : "江苏" }{ "name" : "赵六", "sex" : "男", "age" : 21, "score" : 80, "address" : "山东" }{ "name" : "孙七", "sex" : "女", "age" : 23, "score" : 50, "address" : "湖北" }{ "name" : "tom", "sex" : "男", "age" : 24, "score" : 20, "address" : "海南" }{ "name" : "lucy", "sex" : "女", "age" : 21, "score" : 62, "address" : "浙江" }{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国" }{ "name" : "smith", "sex" : "男", "age" : 19, "score" : 88, "address" : "美国" }
View Code
将年龄为21的score-10db.stu.update({
"age":21},{
"$inc":{
"score":-10}});只修改了一个全部修改score-10 age+10db.stu.update({
"age":21},{
"$inc":{
"score":-10,"age":10}},false,true);

2. $set 内容重置

db.stu.update({
"age":31},{
"$set":{
"score":100}})

3.$unset 删除某个成员内容

{
"$unset":{
"字段":1}}删除tom的年龄和地址db.stu.update({
"name":'tom'},{
"$unset":{
"age":1,"address":1}});db.stu.find({
"name":"tom"},{
"_id":0});{ "name" : "tom", "sex" : "男", "score" : 20 }

4. $push 将内容追加到指定成员中

{
"$push":{
"字段":value}}db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国" }db.stu.update({
"name":"jack"},{
"$push":{
"course":1}})> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ 1 ] }db.stu.update({
"name":"jack"},{
"$push":{
"course":2}}){ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ 1, 2 ] }

5.$pushAll

 

{
"$pushAll":{
"成员":数组}}> db.stu.update({
"name":"jack"},{
"$pushAll":{
"course":[3,4,5]}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ 1, 2, 3, 4, 5 ] }

 

6. $addToSet  向数组里增加内容,不存在时才增加

 

{
"$addToSet":{
"成员":"内容"}}> db.stu.update({
"name":"jack"},{
"$addToSet":{
"course":1}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ 1, 2, 3, 4, 5 ] }> db.stu.update({
"name":"jack"},{
"$addToSet":{
"course":6}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ 1, 2, 3, 4, 5, 6 ] }

 

7. $pop 删除数组内的数据

 

1 (大于0)从尾部出

-1 (小于0)从头部出

> db.stu.update({
"name":"jack"},{
"$pop":{
"course":1}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ 1, 2, 3, 4, 5 ] }> db.stu.update({
"name":"jack"},{
"$pop":{
"course":-1}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ 2, 3, 4, 5 ] }

8.$pull 从数组内删除一个指定的元素(根据值)

db.stu.update({
"name":"jack"},{
"$pull":{
"course":4}});> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ 2, 3 ] }

9.$pullAll  一次性删除多个内容

{$pull:{
"成员":[数据1,数据2 ,...]}}> db.stu.update({
"name":"jack"},{
"$pullAll":{
"course":[2,3,4]}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "sex" : "男", "age" : 20, "score" : 90, "address" : "美国", "course" : [ ] }

10.$rename 重命名成员名称

{
"$rename":{
"旧成员名称":"新成员名称"}}> db.stu.update({
"name":"jack"},{
"$rename":{
"sex":'sex1'}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.stu.find({
"name":"jack"},{
"_id":0});{ "name" : "jack", "age" : 20, "score" : 90, "address" : "美国", "course" : [ ], "sex1" : "男" }

转载于:https://www.cnblogs.com/HKUI/p/6949526.html

你可能感兴趣的文章
《并行计算的编程模型》一2.4.3 阻塞与非阻塞
查看>>
云计算发展的7大关键点
查看>>
零售业的数字机遇:把每个消费者都当成VIP
查看>>
360最新产品亮相全球顶级安全产业大会
查看>>
2014 值得关注的信息安全发展趋势
查看>>
2016年互联网行业十大预测:云计算大数据
查看>>
BlackHat2017热点之DefPloreX---大规模网络犯罪取证的机器学习工具
查看>>
你的企业是否有自动补丁管理工具的潜在需求?
查看>>
阿里云异构计算产品家族亮相 覆盖全场景AI和高性能计算需求
查看>>
巴斯夫如何找到清洁餐具的秘密
查看>>
《逻辑与计算机设计基础(原书第5版)》——第1章 1.0数字系统与信息
查看>>
弃2.4GHz!这就是全新Wi-Fi标准802.11ax
查看>>
BDaas “大数据即服务”的时代即将到来?
查看>>
大数据十大核心问题
查看>>
怎样说服管理者为新的网络产品埋单?
查看>>
SSH如何通过公钥连接云服务器
查看>>
索尼影业就是被这两款工具黑的
查看>>
我想对所有新程序员说的一些话
查看>>
在终端中优雅地编写Python
查看>>
盘点56个最实用的大数据可视化分析工具
查看>>