您好,登錄后才能下訂單哦!
1.$inc
用法:{$inc:{field:value}}
作用:對一個數字字段的某個field增加value
> db.test1.update({'name':'wang'},{$inc:{age:1}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "name" : "wang", "age" : 20, "sex" : "M" }
2.$set
用法:{$set:{field:value}}
作用:把文檔中某個字段field的值設為value
> db.test1.update({'name':'wang'},{$set:{'sex':'F'}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "name" : "wang", "age" : 20, "sex" : "F" }
3.$unset
用法:{$unset:{field:1}}
作用:刪除某個字段field
> db.test1.update({'name':'wang'},{$unset:{'sex':1}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "name" : "wang" }
4.$push
用法:{$push:{field:value}}
作用:把value追加到field里。注:field只能是數組類型,如果field不存在,會自動插入一個數組類型
> db.test1.update({'name':'wang'},{$push:{'sex':'F'}})
> db.test1.update({'name':'wang'},{$push:{'sex':'M'}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "name" : "wang", "sex" : [ "F", "M" ] }
5.pushAll
用法:{$pushAll:{field:value_array}}
作用:用法同$push一樣,只是$pushAll可以一次追加多個值到一個數組字段內。
> db.test1.update({'name':'wang'},{$pushAll:{'sex':['M or F','M and F']}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "name" : "wang", "sex" : [ "F", "M", "M or F", "M and F" ] }
> db.test1.update({'name':'wang'},{$push:{'sex':['M or F','M and F']}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "name" : "wang", "sex" : [ "F", "M", "M or F", "M and F", [ "M or F", "M and F" ] ] }
6.$addToSet
用法:{$addToSet:{field:value}}
作用:加一個值到數組內,而且只有當這個值在數組中不存在時才增加。
> db.test1.update({'name':'wang'},{$addToSet:{'sex':['M or F','MF']}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "name" : "wang", "sex" : [ "F", "M", "M or F", "M and F", [ "M or F", "M and F" ], [ "M or F", "MF" ] ] }
7.$pop
用法:刪除數組內第一個值:{$pop:{field:-1}}、刪除數組內最后一個值:{$pop:{field:1}}
作用:用于刪除數組內的一個值
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "name" : "wang", "sex" : [ "F", "M", "M or F", "M and F" ] }
> db.test1.update({'name':'wang'},{$pop:{'sex':1}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "name" : "wang", "sex" : [ "F", "M", "M or F" ] }
8.$pull
用法:{$pull:{field:_value}}
作用:從數組field內刪除一個等于_value的值
> db.test1.update({'name':'wang'},{$pull:{'sex':"F"}})
> db.test1.find({'name':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "name" : "wang", "sex" : [ "M", "M or F" ] }
10.$rename
用法:{$rename:{old_field_name:new_field_name}}
作用:對字段進行重命名
> db.test1.update({'name':'wang'},{$rename:{'name':'names'}})
> db.test1.find({'name':'wang'})
> db.test1.find({'names':'wang'})
{ "_id" : ObjectId("58662477fb6a734e8f45133f"), "age" : 20, "names" : "wang", "sex" : [ "M", "M or F" ] }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。