mongodb的update

mongodb的update篇


mongodb的update还是可以很强大的

db.collection.update( criteria, objNew, upsert, multi )

  • criteria - query which selects the record to update;
  • objNew - updated object or $ operators (e.g., $inc) which manipulate the object
  • upsert - if this should be an "upsert" operation; that is, if the record(s) do not exist, insert one. Upsert only inserts a single document.            boolean
  • multi - indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.
例子
db.people.update( { name:"Joe" }, {  $set : { x : 1 },$inc: { n : 1 } ,$unset : { field : 1}} );
一些update操作符

{ $push : { field : value } }
向field 字段的array里面 添加 一个value,如果filed字段不是array则报错。

{ $pushAll : { field : value_array } }
和push不同的是添加的 是 array,是多个值。

{ $addToSet : { field : value } }
和push不同的就是 添加 如果是filed的array里面已经存在的value,就不添加

{ $pop : { field : 1  } }  删除array里面最后一个
{ $pop : { field : -1  } }  删除第一个
{ $pull : { field : {field2: value} } } removes array elements with field2 matching value 
比如 { $pull : { groups:{groupName : "groupName"} } } 其中 groupName是groups集合中元素 group的 属性
{ $pull : { field : {$gt: 3} } } removes array elements greater than 3
{ $pull : { field : {<match-criteria>} } } removes array elements meeting match criteria
{ $pullAll : { field : value_array } }
value_array里面主要是符合的都删除

{ $rename : { old_field_name : new_field_name } }

{$bit : { field : {and : 5}}}
{$bit : {field : {or : 43}}}
{$bit : {field : {and : 5, or : 2}}}

Does a bitwise update of field. Can only be used with integers.

The $ positional operator

t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )
The $ operator (by itself) means "position of the matched array item in the query"















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值