MongoDB 更新文檔

來源:互聯網
上載者:User

標籤:base   ase   count   更新   size   存在   over   方法   sql   

update() 方法

update() 方法用於更新已存在的文檔。文法格式如下:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)


參數說明:
query : update的查詢條件,類似sql update查詢內where後面的。
update : update的對象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內set後面的
upsert : 可選,這個參數的意思是,如果不存在update的記錄,是否插入objNew,true為插入,預設是false,不插入。
multi : 可選,mongodb 預設是false,只更新找到的第一條記錄,如果這個參數為true,就把按條件查出來多條記錄全部更新。
writeConcern :可選,拋出異常的層級。


執行個體

我們在集合 mycol 中插入如下資料:

db.mycol.insert({ "_id" : 100,"name":"liang", "title" : "MongoDB Overview" })
db.mycol.insert({ "_id" : 101,"name":"guo", "title" : "MongoDB Guide" })
db.mycol.insert({ "_id" : 102,"name":"jun", "title" : "NoSQL Database" })
db.mycol.insert({ "_id" : 104,"name":"liuzhou", "title" : "Python Quick Guide" })

通過 update() 方法來更新標題(title):
db.mycol.update({"name":"liang"},{$set:{"title":"MongoDB Overview2" }})

db.mycol.find();
{ "_id" : 100, "name" : "liang", "title" : "MongoDB Overview2" }
{ "_id" : 101, "name" : "guo", "title" : "MongoDB Guide" }
{ "_id" : 102, "name" : "jun", "title" : "NoSQL Database" }
{ "_id" : 104, "name" : "liuzhou", "title" : "Python Quick Guide" }


以上語句只會修改第一條發現的文檔,如果你要修改多條相同的文檔,則需要設定 multi 參數為 true。


db.mycol2.insert({ "_id" : 100,"name":"liang", "title" : "MongoDB Overview" })
db.mycol2.insert({ "_id" : 101,"name":"liang", "title" : "MongoDB Guide" })
db.mycol2.insert({ "_id" : 102,"name":"liang", "title" : "NoSQL Database" })
db.mycol2.insert({ "_id" : 104,"name":"liang", "title" : "Python Quick Guide" })

db.mycol2.update({‘name‘:‘liang‘},{$set:{‘title‘:‘MongoDB‘}},{multi:true})
 
db.mycol2.find();
{ "_id" : 100, "name" : "liang", "title" : "MongoDB" }
{ "_id" : 101, "name" : "liang", "title" : "MongoDB" }
{ "_id" : 102, "name" : "liang", "title" : "MongoDB" }
{ "_id" : 104, "name" : "liang", "title" : "MongoDB" }


save() 方法

save() 方法通過傳入的文檔來替換已有文檔。文法格式如下:

db.collection.save(
   <document>,
   {
     writeConcern: <document>
   }
)

db.mycol3.insert({ "_id" : 100,"name":"liang", "title" : "MongoDB" })

db.mycol3.save({ "_id" : 100,"name":"liang2", "title" : "MongoDB2" })


db.mycol3.find()
{ "_id" : 100, "name" : "liang2", "title" : "MongoDB2" }


更多執行個體

只更新第一條記錄:
db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );

全部更新:
db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );

只添加第一條:
db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );

全部添加加進去:
db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );

全部更新:
db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );

只更新第一條記錄:
db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );



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.