MongoDB常用操作---更新update方法-創(chuàng)新互聯(lián)

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

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

mongodb兩個更新命令

成都創(chuàng)新互聯(lián)從2013年開始,先為深州等服務(wù)建站,深州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為深州企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
  1. update
    db.collection.update( criteria, objNew, upsert, multi )
    criteria : update的查詢條件,類似sql update查詢內(nèi)where后面的
    objNew : update的對象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內(nèi)set后面的
    upsert : 這個參數(shù)的意思是,如果不存在update的記錄,是否插入objNew,true為插入,默認(rèn)是false,不插入。
    multi : mongodb默認(rèn)是false,只更新找到的第一條記錄,如果這個參數(shù)為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 ); 只加進(jìn)去了第一條
    db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全加進(jìn)去了
    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內(nèi)已經(jīng)存在一個和x對象相同的"_id"的記錄。mongodb就會把x對象替換collection內(nèi)已經(jīng)存在的記錄,否則將會插入x對象,如果x內(nèi)沒有_id,系統(tǒng)會自動生成一個再插入。相當(dāng)于上面update語句的upsert=true,multi=false的情況。
    例:

     db.test0.save({count:40,test1:"OK"}); #_id系統(tǒng)會生成
     db.test0.save({_id:40,count:40,test1:"OK"}); #如果test0內(nèi)有_id等于40的,會替換,否則插入。

mongodb的更新操作符

  1. inc
    用法:{ $inc : { field : value } }
    對一個數(shù)字字段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} }
    刪除字段(注意不是刪除行數(shù)據(jù))

     db.test0.update( { "storeId":{ $gt : 5500 }} , { $unset : { "type" : "hello"} } );
  3. push
    用法:{ $push : { field : value } }
    把value追加到field里面去,
    如果field存在,則一定要是數(shù)組類型才行(否則會報錯:The field 'test1' must be an array but is of type String in document)
    如果field不存在,會新增一個數(shù)組類型加進(jìn)去。

     db.test0.update( { "_id" : 15 } , { $push  : { "test1" : ["aaa"] } } );
  4. pushAll
    用法:{ $pushAll : { field : value_array } }
    同$push,只是一次可以追加多個值到一個數(shù)組字段內(nèi)。(否則會報錯:$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 } }
    增加一個值到數(shù)組內(nèi),而且只有當(dāng)這個值不在數(shù)組內(nèi)才增加。

     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 } }
    從數(shù)組field內(nèi)刪除一個等于value值。 (所有等于value的都會刪除)

     db.test0.update( { "_id" : 15 } , { $pull : { "test1": "ggg" } } );
  8. pullAll
    用法:{ $pullAll : { field : value_array } }
    同$pull,可以一次刪除數(shù)組內(nèi)的多個值。(所有等于value的都會刪除)

    db.test0.update( { "_id" : 15 } , { $pullAll : { "test1": [ "ccc" , "fff" ] } } );
  9. $操作符
    $是他自己的意思,代表按條件找出的數(shù)組里面某項他自己。呵呵,比較坳口??匆幌鹿俜降睦樱?/p>

    > 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 } ] }
    
    需要注意的是,$只會應(yīng)用找到的第一條數(shù)組項,后面的就不管了。還是看例子:
    > t.find();
    { "_id" : ObjectId("4b9e4a1fc583fa1c76198319"), "x" : [ 1, 2, 3, 2 ] }
    > t.update({x: 2}, {$inc: {"x.$": 1}}, false, true);
    > t.find();
    
    還有注意的是$配合$unset使用的時候,會留下一個null的數(shù)組項,不過可以用{$pull:{x:null}}刪除全部是null的數(shù)組項。例:
    > 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 ] }

網(wǎng)站題目:MongoDB常用操作---更新update方法-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://bm7419.com/article20/cdjsco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)網(wǎng)站排名、動態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、用戶體驗、虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司