db.test1.update({ name : wang },{$inc:{age:1}})> db.test1.find({ name : wang..."/>
您好,登錄后才能下訂單哦!
1.$inc
用法:{$inc:{field:value}}
作用:對一個數(shù)字字段的某個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的值設(shè)為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只能是數(shù)組類型,如果field不存在,會自動插入一個數(shù)組類型
> 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可以一次追加多個值到一個數(shù)組字段內(nèi)。
> 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}}
作用:加一個值到數(shù)組內(nèi),而且只有當(dāng)這個值在數(shù)組中不存在時才增加。
> 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
用法:刪除數(shù)組內(nèi)第一個值:{$pop:{field:-1}}、刪除數(shù)組內(nèi)最后一個值:{$pop:{field:1}}
作用:用于刪除數(shù)組內(nèi)的一個值
> 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}}
作用:從數(shù)組field內(nèi)刪除一個等于_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}}
作用:對字段進(jìn)行重命名
> 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" ] }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。