這篇文章主要介紹MongoDB中$inc和$set有什么不同之處,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有成都做網(wǎng)站、成都網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷策劃、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號(hào)搭建、小程序開發(fā)、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動(dòng)行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
1、$inc
這個(gè)修改器干什么使的呢?看看下面示例的具體操作后的結(jié)果即可知道。
示例文檔:{"uid":"201203","type":"1",size:10}
> db.b.insert({"uid":"201203","type":"1",size:10}) > db.b.find() { "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1", "size" : 10 } > db.b.update({"uid" : "201203"},{"$inc":{"size" : 1}}) > db.b.find() { "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1", "size" : 11 } > db.b.update({"uid" : "201203"},{"$inc":{"size" : 2}}) > db.b.find() { "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1", "size" : 13 } > db.b.update({"uid" : "201203"},{"$inc":{"size" : -1}}) > db.b.find() { "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1", "size" : 12 }
得出結(jié)論:修改器$inc可以對(duì)文檔的某個(gè)值為數(shù)字型(只能為滿足要求的數(shù)字)的鍵進(jìn)行增減的操作。
(這里有個(gè)問(wèn)題:上篇中說(shuō)到更新默認(rèn)只對(duì)滿足條件的記錄集中第一個(gè)文檔進(jìn)行更新,那么使用$inc修改器之后,還是一樣嗎?)
2、$set
用來(lái)指定一個(gè)鍵并更新鍵值,若鍵不存在并創(chuàng)建。來(lái)看看下面的效果:
> db.a.findOne({"uid" : "20120002","type" : "3"}) { "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num" : 40, "sname" : "jk", "type" : "3", "uid" : "20120002" } --size鍵不存在的場(chǎng)合 > db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"size":10}}) > db.a.findOne({"uid" : "20120002","type" : "3"}) { "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num" : 40, "size" : 10, "sname" : "jk", "type" : "3", "uid" : "20120002" } --sname鍵存在的場(chǎng)合 > db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":"ssk"}}) > db.a.find() { "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num" : 40, "size" : 10, "sname" : "ssk", "type" : "3", "uid" : "20120002" } { "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num" : 50, "sname" : "jk", "type" : "1", "uid" : "20120002" } --可改變鍵的值類型 > db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":["Java",".net","c++"]}}) > db.a.findOne({"uid" : "20120002","type" : "3"}) { "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num" : 40, "size" : 10, "sname" : [ "java", ".net", "c++" ], "type" : "3", "uid" : "20120002" }
對(duì)于內(nèi)嵌的文檔,$set又是如何進(jìn)行更新的內(nèi)嵌的文檔的呢,請(qǐng)看下面的示例:
示例文檔:{"name":"toyota","type":"suv","size":{"height":10,"width":5,"length":15}}
> db.c.findOne({"name":"toyota"}) { "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "type" : "suv", "size" : { "height" : 10, "width" : 5, "length" : 15 } } > db.c.update({"name":"toyota"},{"$set":{"size.height":8}}) > db.c.findOne({"name":"toyota"}) { "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "type" : "suv", "size" : { "height" : 8, "width" : 5, "length" : 15 } } > db.c.update({"name":"toyota"},{"$set":{"size.width":7}}) > db.c.findOne({"name":"toyota"}) { "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "type" : "suv", "size" : { "height" : 8, "width" : 7, "length" : 15 } }
可見:對(duì)于內(nèi)嵌文檔在使用$set更新時(shí),使用"."連接的方式。
以上是mongodb中$inc和$set有什么不同之處的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!