数据更新
简单的做法是删除重新插入
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岁的分数更新为60 db.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" : "美国" }
将年龄为21的score-10 db.stu.update({"age":21},{"$inc":{"score":-10}}); 只修改了一个 全部修改score-10 age+10 db.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" : "男" }