$inc:增加或者減少已有的鍵值(如果不存在則新建一個(gè))
用法:
db.collection.update({},{"$inc":{"key":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"}})
- key: 就是key
- 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"使用