数据更新
简单的做法是删除重新插入
update()函数
语法
db.集合.update(更新条件,新的对象数据(更新操作符),upsert,multi)
upsert如果要更新的数据不存在,则增加一条新的内容(true为增加,false不增加)
multi:是否只更新满足条件的第一条记录,设置为false只更行第一个,true全更新
> db.stu.find({},{"_id":})
{ "name" : "张三", "sex" : "男", "age" : , "score" : , "address" : "河南" }
{ "name" : "李四", "sex" : "女", "age" : , "score" : , "address" : "山东" }
{ "name" : "王五", "sex" : "男", "age" : , "score" : , "address" : "江苏" }
{ "name" : "赵六", "sex" : "男", "age" : , "score" : , "address" : "山东" }
{ "name" : "孙七", "sex" : "女", "age" : , "score" : , "address" : "湖北" }
{ "name" : "tom", "sex" : "男", "age" : , "score" : , "address" : "海南" }
{ "name" : "lucy", "sex" : "女", "age" : , "score" : , "address" : "浙江" }
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国" }
{ "name" : "smith", "sex" : "男", "age" : , "score" : , "address" : "美国" } #.把年龄<20岁的分数更新为60
db.stu.find({"age":{"$lt":}},{"_id":});
{ "name" : "张三", "sex" : "男", "age" : , "score" : , "address" : "河南" }
{ "name" : "王五", "sex" : "男", "age" : , "score" : , "address" : "江苏" }
{ "name" : "smith", "sex" : "男", "age" : , "score" : , "address" : "美国" } db.stu.update({"age":{"$lt":}},{"$set":{"score":}},false,false);
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
#只修改了一个
db.stu.find({"age":{"$lt":}},{"_id":});
{ "name" : "张三", "sex" : "男", "age" : , "score" : , "address" : "河南" }
{ "name" : "王五", "sex" : "男", "age" : , "score" : , "address" : "江苏" }
{ "name" : "smith", "sex" : "男", "age" : , "score" : , "address" : "美国" }
#全部修改
db.stu.update({"age":{"$lt":}},{"$set":{"score":}},false,true);
db.stu.find({"age":{"$lt":}},{"_id":});
{ "name" : "张三", "sex" : "男", "age" : , "score" : , "address" : "河南" }
{ "name" : "王五", "sex" : "男", "age" : , "score" : , "address" : "江苏" }
{ "name" : "smith", "sex" : "男", "age" : , "score" : , "address" : "美国" } save() db.stu.save({"_id" : ObjectId("592cc2e00be561e9e879bcb5"),"name":'张三三'});
只能根据_id更新
修改器
1.$inc :针对于一个数字字段,修改数字字段内容
语法: {"$inc":{"字段":步调}}
db.stu.find({},{"_id":});
{ "name" : "张三", "sex" : "男", "age" : , "score" : , "address" : "河南" }
{ "name" : "李四", "sex" : "女", "age" : , "score" : , "address" : "山东" }
{ "name" : "王五", "sex" : "男", "age" : , "score" : , "address" : "江苏" }
{ "name" : "赵六", "sex" : "男", "age" : , "score" : , "address" : "山东" }
{ "name" : "孙七", "sex" : "女", "age" : , "score" : , "address" : "湖北" }
{ "name" : "tom", "sex" : "男", "age" : , "score" : , "address" : "海南" }
{ "name" : "lucy", "sex" : "女", "age" : , "score" : , "address" : "浙江" }
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国" }
{ "name" : "smith", "sex" : "男", "age" : , "score" : , "address" : "美国" }
将年龄为21的score-
db.stu.update({"age":},{"$inc":{"score":-}});
只修改了一个 全部修改score- age+
db.stu.update({"age":},{"$inc":{"score":-,"age":}},false,true);
2. $set 内容重置
db.stu.update({"age":},{"$set":{"score":}})
3.$unset 删除某个成员内容
{"$unset":{"字段":}}
删除tom的年龄和地址
db.stu.update({"name":'tom'},{"$unset":{"age":,"address":}});
db.stu.find({"name":"tom"},{"_id":});
{ "name" : "tom", "sex" : "男", "score" : }
4. $push 将内容追加到指定成员中
{"$push":{"字段":value}} db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国" }
db.stu.update({"name":"jack"},{"$push":{"course":}})
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ ] }
db.stu.update({"name":"jack"},{"$push":{"course":}})
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ , ] }
5.$pushAll
{"$pushAll":{"成员":数组}}
> db.stu.update({"name":"jack"},{"$pushAll":{"course":[,,]}})
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ , , , , ] }
6. $addToSet 向数组里增加内容,不存在时才增加
{"$addToSet":{"成员":"内容"}} > db.stu.update({"name":"jack"},{"$addToSet":{"course":}});
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ , , , , ] } > db.stu.update({"name":"jack"},{"$addToSet":{"course":}});
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ , , , , , ] }
7. $pop 删除数组内的数据
1 (大于0)从尾部出
-1 (小于0)从头部出
> db.stu.update({"name":"jack"},{"$pop":{"course":}});
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ , , , , ] }
> db.stu.update({"name":"jack"},{"$pop":{"course":-}});
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ , , , ] }
8.$pull 从数组内删除一个指定的元素(根据值)
db.stu.update({"name":"jack"},{"$pull":{"course":}});
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ , ] }
9.$pullAll 一次性删除多个内容
{$pull:{"成员":[数据1,数据2 ,...]}}
> db.stu.update({"name":"jack"},{"$pullAll":{"course":[,,]}});
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "sex" : "男", "age" : , "score" : , "address" : "美国", "course" : [ ] }
10.$rename 重命名成员名称
{"$rename":{"旧成员名称":"新成员名称"}}
> db.stu.update({"name":"jack"},{"$rename":{"sex":'sex1'}});
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
> db.stu.find({"name":"jack"},{"_id":});
{ "name" : "jack", "age" : , "score" : , "address" : "美国", "course" : [ ], "sex1" : "男" }