MongoDB學習筆記(二)

來源:互聯網
上載者:User

標籤:

MongoDB基本操作之Update

update方法參數

//query:更新的條件//obj:更新的對象//Boolean類型,true:沒有則新增,false:只更新不新增//multi:是否允許批次更新function (query, obj, upsert, multi) {    //dosomething}
View Code

文檔替換:

var where={    "age":29};var p1 = db.user.findOne(where);p1.age++;//年齡自增1db.user.update(where,p1);//將匹配到的第一個文檔整個替換成新的文檔
View Code

常用修改器:

  1. "$set"修改器:$set修改器用於修改文檔中的某個欄位的值,如果該欄位不存在則會新增該欄位。
    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$set":{ "hobby":"跑步" }//$set修改器會更新或者新增指定的欄位}db.user.update(where,action);//為鄧超新增一個hoppy欄位
    View Code

    修改欄位的類型

    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$set":{"hoppy":["跑步","唱歌","跳舞"]}//將之前的hoppy欄位修改成一個興趣數組}db.user.update(where,action);
    View Code

    修改內嵌文檔中的欄位

    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$set":    {        "family":        {            "wife":"孫儷",            "father":"未知",            "monther":"未知",            "sons":["鄧涵之","鄧涵一"]        }    }};db.user.update(where,action);//增加鄧超家庭關係資料action={    "$set":{"family.father":"鄧九公"}//將鄧超關係文檔中的父親欄位改為鄧九公}db.user.update(where,action);
    View Code

    刪除文檔中的某個欄位

    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$unset":{"family.wife":1}//$unset操作符用於移除某個欄位};db.user.update(where,action);
    View Code
  2. "$inc"修改器:自增或自減修改器,原子性操作。
    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$inc":{"age":1}//年齡自增1,-1則為自減,該操作符只能作用於數實值型別,否則會出異常}db.user.update(where,action);db.user.find(where);
    View Code
  3. "$push"操作符:向一個數組的末尾添加一個元素,如果指定數組不存在,則該數組會被建立
    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$push":{"family.sons":"鄧紫棋"}//往sons數組尾部添加一個元素,如果sons數組不存在,則會被建立};db.user.update(where,action);
    View Code
  4. "$each"操作符:$push操作符配合$each操作符能實現一次往數組中添加多個元素
    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$push":{"family.sons":{"$each":["鄧紫棋","鄧小山","鄧九妹"]}}//將數組中的元素迴圈添加到指定數組中};db.user.update(where,action);
    View Code
  5. "$slice"操作符:用於設定數組的長度,當值為正數時,只保留數組中的前N個元素,為負數時,保留數組中的最後N個元素
    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$push":{"family.sons":{"$each":["鄧紫棋","鄧涵一","鄧涵之"],"$slice":-2}}//將數組中的元素迴圈添加到指定數組中};db.user.update(where,action);
    View Code
  6. "$sort"操作符:對數組中的元素進行排序
    {    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$push":{"family.sons":{"$each":["鄧紫棋","鄧涵一","鄧涵之"],"$sort":-1}}//排序可以按照某個欄位排,也可以指定直接指定-1,0或1};db.user.update(where,action);
    View Code
  7. "$addToSet"操作符:往數組中添加新元素且只有當數組中不存在的時候才會新增
    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$addToSet":{"family.sons":{"$each":["鄧紫棋","鄧涵夕"],"$sort":-1}}//addToSet只有當數組中沒有該元素的時候才會往裡面新增};db.user.update(where,action);
    View Code
  8. "$pop"操作符:從數組頂部或尾部刪除元素,可用於類比隊列或者棧
    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$pop":{"family.sons":-1}//1:從數組頭部刪除一個元素,-1從數組尾部刪除一個元素};db.user.update(where,action);
    View Code
  9. "$pull"操作符:刪除數組中的所有匹配元素
    var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$pull":{"family.sons":"鄧涵一"}//刪除指定數組中的所有的指定元素};db.user.update(where,action);db.runCommand("getLastError");
    View Code
  10. 通過數組元素位置修改元素:
  11. var where={    "_id":ObjectId("5634b21827480baffb199df4")};var action={    "$inc":{"age":-5},//年齡自減5    "$set":{"family.sons.0":"鄧紫棋"},//直接通過下標的形式修改sons數組中的第一個元素的值為:鄧紫棋};db.user.update(where,action);
    View Code

    使用$定位:

    var where={    "family.sons.name":"鄧紫棋"};var action={    //$符號自動定位匹配到的數組元素,例如第一個匹配到的數組元素下標為3,那麼$實際上可以認為就是3    "$set":{"family.sons.$.name":"鄧小山"},};db.user.update(where,action);
    View Code 
  12. findAndModify函數:更新並返回某個文檔
    db.user.findAndModify({    query: {name: ‘a2‘}, //查詢條件    sort: {age: -1}, //排序方式    update: {$set: {name: ‘張傑‘}, $inc: {age: 2}},//更新,或使用remove:true執行刪除    new:true//返回更新後的文檔});
    View Code

     

     

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.