MongoDB修改器

$inc:增加或者減少已有的鍵值(如果不存在則新建一個(gè))

用法:

db.collection.update({},{"$inc":{"key":1}})
  1. key:需要更新的鍵(不存在則新建一個(gè)),后面的值可以為正數(shù)或者負(fù)數(shù),表示增加或者減少。
    PS: 不管該鍵值對(duì)存在與否,值只能是數(shù)值類型(e.g.浮點(diǎn),整數(shù))。
例子:
db.foo.insert({"count":1})
db.foo.find()
{ "_id" : ObjectId("5a4ce27c50ac11b145adb170"), "count" : 1 }

//use $inc
db.foo.update({},{"$inc":{"count":-1}})
db.foo.find()
{ "_id" : ObjectId("5a4ce27c50ac11b145adb170"), "count" : 0 }

$set: 用來指定一個(gè)字段的值(不存在則新建)。

用法:

db.collection.update({},{"$set":{"key":"value"}})
  1. key: 就是key
  2. value:就是value
例子:
db.foo.insert({"Title":"What the hell"})
db.foo.find()
{ "_id" : ObjectId("5a4ce50350ac11b145adb171"), "Title" : "What the hell" }

// use $set
db.foo.update({},{"$set":{"Content":"The hell is the code"}})
db.foo.find()
{ "_id" : ObjectId("5a4ce50350ac11b145adb171"), "Title" : "What the hell", "Content" : "The hell is the code"

$unset: 跟$set相反,刪除某個(gè)字段。

用法:

db.collection.update({},{"$unset":{"key":"value"}})
例子:
//Depend on the exampe above
db.foo.update({},{"$unset":{"Content":""}})
db.foo.find()
{ "_id" : ObjectId("5a4ce50350ac11b145adb171"), "Title" : "What the hell" }

$push: 如果數(shù)組已經(jīng)存在,"$push"會(huì)向已有數(shù)組末尾加入一個(gè)元素,如果沒有則創(chuàng)建一個(gè)。

用法:

db.collection.update({},{"$push":{"key":{"key":"value","key1":"value1" ...}}})
例子:
db.user.insert({"name":"Chan"})
db.user.find()
{ "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan" }

// use $push
db.user.update({},{"$push":{"favourite":{"f_name":"eat","f_each_day":3}}})
db.user.find()
{ "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan", "favourite" : [ { "f_name" : "eat", "f_each_day" : 3 } ] }

子操作符$each:可以通過push一次性添加多個(gè)值:

用法:

db.collection.update({},{"$push":{"key":{"$each":["value1","value2","value3"]}}})
例子:
//Depend on the exampe above
db.user.update({},{"$push":{"hair":{"$each":["black","ken","aye"]}}})
db.user.find()
{ "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan", "favourite" : [ { "f_name" : "eat", "f_each_day" : 3 } ], "hair" : [ "black", "ken", "aye" ] }

當(dāng)然你也可以兩個(gè)混合起來用:

db.user.update({},{"$push":{"eye":{"$each":[{"l":"this is l","xyz":"this is xyz"},{"m":"this is m","abc":888.668}]}}})
db.user.find()
{ "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan", "favourite" : [ { "f_name" : "eat", "f_each_day" : 3 } ], "hair" : [ "black", "ken", "aye" ], "eye" : [ { "l" : "this is l", "xyz" : "this is xyz" }, { "m" : "this is m", "abc" : 888.668 } ] }

PS:如果"$each"指定的數(shù)組里面只有一個(gè)元素,則操作等同于普通"$push"操作。

子操作符$slice

用法:

db.collection.update({},{"$push":{"key":{"$each":["value1,"value2","value3"],"$slice":10}}})

以上會(huì)只保留數(shù)組里面的最前面10個(gè)元素,假如值是1-20,則只保留1-10.

例子:
db.user.update({},{"$push":{"face":{"$each":[1,2,3,4,5,6,7,8,9,10],"$slice":3}}})
 db.user.find()
{ "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan", "face" : [ 1, 2, 3 ] }

如果$slice的值為負(fù)整數(shù),則只保留后面幾個(gè)值:

db.user.update({},{"$push":{"skin":{"$each":["a","b","c","d","e","f","g","h"],"$slice":-3}}})
db.user.find()
{ "_id" : ObjectId("5a4cec5b50ac11b145adb173"), "name" : "chan", "skin" : [ "f", "g", "h" ] }

子操作符$sort:字面意思,按照指定的鍵排序:

用法:

db.collection.update({},{"$push":{"key":{"$each":[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}],"$sort":{"key2":-1}}}})

PS:"$sort":{"key2":-1} 值為-1則是降序,1就是升序。(不允許為其他值)

例子:
 db.user.update({},{"$push":{"skin":{"$each":[{"name":"chan","raiting":10},{"name":"zee","raiting":-5},{"name":"bm","raiting":30}],"$sort":{"raiting":1}}}})
db.user.find()
{ "_id" : ObjectId("5a4cec5b50ac11b145adb173"), "name" : "chan", "skin" : [{ "name" : "zee", "raiting" : -5 }, { "name" : "chan", "raiting" : 10 }, { "name" : "bm", "raiting" : 30 } ] }

"$slice","$sort"只能搭配"$each"使用,不能直接跟"$push"使用

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.字段修改器 $currentDate數(shù)據(jù)準(zhǔn)備:db.userlist.insert({name:'mily',...
    Eve0閱讀 598評(píng)論 0 0
  • 關(guān)于operators 官方參考文檔:https://docs.mongodb.org/manual/refere...
    我看不見閱讀 2,747評(píng)論 0 3
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對(duì)象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,682評(píng)論 0 4
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,609評(píng)論 19 139
  • 今晚又與父母吵架,翻來覆去地睡不著。在父母面前我永遠(yuǎn)是不善表達(dá)的,可能很多想法,只有通過文字才能將我的意愿攤開讓他...
    懶得寫字的人閱讀 336評(píng)論 0 4

友情鏈接更多精彩內(nèi)容