• MongoDB常用操作---更新update方法


    MongoDB常用操作---更新update方法

    (来源:MongoDB常用操作一更新update方法)

    mongodb两个更新命令

    1. update
      db.collection.update( criteria, objNew, upsert, multi )
      criteria : update的查询条件,类似sql update查询内where后面的
      objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
      upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
      multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
      例:

      db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一条记录
      db.test0.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 全更新了
      db.test0.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 只加进去了第一条
      db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全加进去了
      db.test0.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );全更新了
      db.test0.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );只更新了第一条

    2. save
      db.collection.save( x ) x就是要更新的对象,只能是单条记录。
      如果在collection内已经存在一个和x对象相同的"_id"的记录。mongodb就会把x对象替换collection内已经存在的记录,否则将会插入x对象,如果x内没有_id,系统会自动生成一个再插入。相当于上面update语句的upsert=true,multi=false的情况。
      例:

       db.test0.save({count:40,test1:"OK"}); #_id系统会生成
       db.test0.save({_id:40,count:40,test1:"OK"}); #如果test0内有_id等于40的,会替换,否则插入。
      

    mongodb的更新操作符

    1. inc
      用法:{ $inc : { field : value } }
      对一个数字字段field增加value

       db.test0.update( { "storeId":{ $gt : 5500 }} , { $inc : { "storeId" : 20} } );
       db.test0.update( { "storeId":{ $gt : 5500 }} , { $inc : { "storeId" : -20} } );
      
    2. unset
      用法:{ $unset : { field : 1} }
      删除字段(注意不是删除行数据)

       db.test0.update( { "storeId":{ $gt : 5500 }} , { $unset : { "type" : "hello"} } );
      
    3. push
      用法:{ $push : { field : value } }
      把value追加到field里面去,
      如果field存在,则一定要是数组类型才行(否则会报错:The field 'test1' must be an array but is of type String in document)
      如果field不存在,会新增一个数组类型加进去。

       db.test0.update( { "_id" : 15 } , { $push  : { "test1" : ["aaa"] } } );
      
    4. pushAll
      用法:{ $pushAll : { field : value_array } }
      同$push,只是一次可以追加多个值到一个数组字段内。(否则会报错:$pushAll requires an array of values but was given an String)

       db.test0.update( { "_id" : 15 } , { $pushAll  : { "test1" : ["aaa","bbb"] } } );
      
    5. addToSet
      用法:{ $addToSet : { field : value } }
      增加一个值到数组内,而且只有当这个值不在数组内才增加。

       db.test0.update( { "storeId":{ $gt : 5600 }} , { $addToSet :{ "test1": "vvv" } } );
      
    6. pop
      删除最后一个值:{ $pop : { field : 1 } }删除第一个值:{ $pop : { field : -1 } }
      注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条。mongodb 1.1及以后的版本才可以用

       db.test0.update( { "storeId":{ $gt : 5600 }} ,  { $pop : { "test1": -1 } }  );
      
    7. pull
      用法:$pull : { field : value } }
      从数组field内删除一个等于value值。 (所有等于value的都会删除)

       db.test0.update( { "_id" : 15 } , { $pull : { "test1": "ggg" } } );
      
    8. pullAll
      用法:{ $pullAll : { field : value_array } }
      同$pull,可以一次删除数组内的多个值。(所有等于value的都会删除)

      db.test0.update( { "_id" : 15 } , { $pullAll : { "test1": [ "ccc" , "fff" ] } } );
      
    9. $操作符
      $是他自己的意思,代表按条件找出的数组里面某项他自己。呵呵,比较坳口。看一下官方的例子:

      > t.find()
      { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }
      
      > t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )
      
      > t.find()
      { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }
      
      需要注意的是,$只会应用找到的第一条数组项,后面的就不管了。还是看例子:
      > t.find();
      { "_id" : ObjectId("4b9e4a1fc583fa1c76198319"), "x" : [ 1, 2, 3, 2 ] }
      > t.update({x: 2}, {$inc: {"x.$": 1}}, false, true);
      > t.find();
      
      还有注意的是$配合$unset使用的时候,会留下一个null的数组项,不过可以用{$pull:{x:null}}删除全部是null的数组项。例:
      > t.insert({x: [1,2,3,4,3,2,3,4]})
      > t.find()
      { "_id" : ObjectId("4bde2ad3755d00000000710e"), "x" : [ 1, 2, 3, 4, 3, 2, 3, 4 ] }
      > t.update({x:3}, {$unset:{"x.$":1}})
      > t.find()
      { "_id" : ObjectId("4bde2ad3755d00000000710e"), "x" : [ 1, 2, null, 4, 3, 2, 3, 4 ] }
      > {$pull:{x:null}}
      { "_id" : ObjectId("4b9e4a1fc583fa1c76198319"), "x" : [ 1, 3, 3, 2 ] }
  • 相关阅读:
    使用SDL2显示一张图片,SDL2上手贴
    两种方法操作其它mac应用的窗口
    golang子进程的启动和停止,mac与linux的区别
    自己动手,装一个液晶电视
    在Ubuntu上搭建kindle gtk开发环境
    macOS的OpenCL高性能计算
    量子计算及量子计算的模拟
    iPhone多次输入错误密码锁机后刷机恢复(原有内容会丢失)
    Day 2 总结
    Day 13 linux 的输入输出与管道的使用
  • 原文地址:https://www.cnblogs.com/cuiyf/p/6669496.html
Copyright © 2020-2023  润新知