mongoDB資料更新與操作符

來源:互聯網
上載者:User

標籤:push   使用   自己的   nbsp   更新   官方   tle   存在   upd   

//轉載

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.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 16,"test1" : "TESTTEST", "test2" : "OK","test3" : "TESTTEST", "test4" : "OK","test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $inc : { "count" :1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 17,"test1" : "TESTTEST", "test2" : "OK","test3" : "TESTTEST", "test4" : "OK","test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $inc : { "count" :2 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 19,"test1" : "TESTTEST", "test2" : "OK","test3" : "TESTTEST", "test4" : "OK","test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $inc : { "count" :-1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : "TESTTEST", "test2" : "OK","test3" : "TESTTEST", "test4" : "OK","test5" : "OK" }


2) $set

用法:{ $set : { field : value } }

就是相當於sql的set field = value,全部資料類型都支援$set。例:
> db.test0.update( { "_id" : 15 } , { $set : { "test1" :"testv1","test2" : "testv2","test3" :"testv3","test4" : "testv4" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : "testv1", "test2" : "testv2","test3" : "testv3", "test4" : "testv4","test5" : "OK" }

3) $unset

用法:{ $unset : { field : 1} }

顧名思義,就是刪除欄位了。例:
> db.test0.update( { "_id" : 15 } , { $unset : {"test1":1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test2" : "testv2", "test3" : "testv3","test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $unset : {"test2": 0 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test3" : "testv3", "test4" : "testv4","test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $unset : {"test3":asdfasf } } );
Fri May 14 16:17:38 JS Error: ReferenceError: asdfasf is not defined (shell):0

> db.test0.update( { "_id" : 15 } , { $unset : { "test3":"test"} } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test4" : "testv4", "test5" : "OK" }

沒看出field : 1裡面的1是幹什麼用的,反正只要有東西就行。


4) $push


用法:{ $push : { field :value } }

把value追加到field裡面去,field一定要是數群組類型才行,如果field不存在,會新增一個數群組類型加進去。例:

> db.test0.update( { "_id" : 15 } , { $set : { "test1" :["aaa","bbb"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa", "bbb" ], "test4" :"testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $push : { "test1":"ccc" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa", "bbb", "ccc" ],"test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $push : { "test2":"ccc" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa", "bbb", "ccc" ],"test2" : [ "ccc" ], "test4" :"testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $push : { "test1":["ddd","eee"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa", "bbb", "ccc", ["ddd", "eee" ] ], "test2" : [ "ccc" ],"test4" : "testv4", "test5" : "OK" }

 

5) $pushAll



用法:{ $pushAll : { field : value_array } }

同$push,只是一次可以追加多個值到一個數組欄位內。例:

> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa", "bbb", "ccc", ["ddd", "eee" ] ], "test2" : [ "ccc" ],"test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $pushAll : {"test1": ["fff","ggg"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa", "bbb", "ccc", ["ddd", "eee" ], "fff", "ggg" ],"test2" : [ "ccc" ], "test4" :"testv4", "test5" : "OK" }

6)  $addToSet


用法:{ $addToSet : {field : value } }

增加一個值到數組內,而且只有當這個值不在數組內才增加。例:
> db.test0.update( { "_id" : 15 } , { $addToSet : {"test1": {$each : ["444","555"] } } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa", "bbb" , "ccc", [ "ddd",  "eee" ], "fff", "ggg", [ "111", "222" ],"444", "555"], "test2" : [ "ccc" ], "test4" :"testv4", "test5" : "OK" }


> db.test0.update( { "_id" : 15 } , { $addToSet : {"test1": {$each : ["444","555"] } } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : ["aaa", "bbb", "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ], "444", "555" ], "test2" : [ "ccc" ], "test4" :"testv4", "test5" : "OK" }


> db.test0.update( { "_id" : 15 } , { $addToSet : {"test1": ["444","555"]  } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa","bbb", "ccc", ["ddd", "eee" ], "fff", "ggg", [ "111", "222" ], "444", "555", [ "444", "555"]], "test2" : [ "ccc" ], "test4" : testv4", "test5" : "OK" }


> db.test0.update( { "_id" : 15 } , { $addToSet : {"test1": ["444","555"]  } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ], "444", "555", [ "444", "555" ]], "test2" : [ "ccc" ], "test4" :"testv4", "test5" : "OK" }


7) $pop

刪除數組內的一個值

用法:
刪除最後一個值:{ $pop : { field : 1  } }

刪除第一個值:{ $pop : {field : -1  } }

注意,只能刪除一個值,也就是說只能用1或-1,而不能用2或-2來刪除兩條。mongodb1.1及以後的版本才可以用,例:
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "bbb", "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ], "444"], "test2" : [ "ccc" ], "test4" :"testv4", "test5" : "OK" }


> db.test0.update( { "_id" : 15 } , { $pop : { "test1":-1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222"], "444"], "test2" : [ "ccc" ], "test4" :"testv4", "test5" : "OK" }


> db.test0.update( { "_id" : 15 } , { $pop : { "test1":1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "ccc", [ "ddd", "eee" ],"fff", "ggg", [ "111", "222" ] ],"test2" : [ "ccc" ], "test4" :"testv4",
"test5" : "OK" }

8) $pull

用法:$pull : { field : value } }

從數組field內刪除一個等於value值。例:
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "ccc", [ "ddd", "eee" ],"fff", "ggg", [ "111", "222" ] ],"test2" : [ "ccc" ], "test4" :"testv4",
"test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $pull : { "test1":"ggg" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "ccc", [ "ddd", "eee" ],"fff", [ "111", "222" ] ], "test2" : ["ccc" ], "test4" : "testv4", "test5"
 : "OK" }

9) $pullAll

用法:{ $pullAll : { field : value_array } }

同$pull,可以一次刪除數組內的多個值。例:
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ "ccc", [ "ddd", "eee" ],"fff", [ "111", "222" ] ], "test2" : ["ccc" ], "test4" : "testv4", "test5"
 : "OK" }

> db.test0.update( { "_id" : 15 } , { $pullAll : {"test1": [ "ccc" , "fff" ] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18,"test1" : [ [ "ddd", "eee" ], [ "111","222" ] ], "test2" : [ "ccc" ], "test4": "testv4", "test5" : "OK" }


10) $ 操作符

$是他自己的意思,代表按條件找出的數組裡面某項他自己。呵呵,比較坳口。看一下官方的例子:

> 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 ] }

{ "_id" : ObjectId("4b9e4a1fc583fa1c76198319"),"x" : [ 1, 3, 3, 2 ] }

mongoDB資料更新與操作符

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.