新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1.$inc
创新互联建站主营竹山网站建设的网络公司,主营网站建设方案,手机APP定制开发,竹山h5重庆小程序开发搭建,竹山网站营销推广欢迎竹山等地区企业咨询
用法:{$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" ] }