mongoDB Update文档

MongoDB supports atomic, in-place updates as well as more traditional updates which replace an entire document.

update()

update() replaces the document matching criteria entirely with objNew. If you only want to modify some fields, you should use the $ modifiers below.

Here's the MongoDB shell syntax for update() :

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

Arguments:

  • 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 .
  • multi - indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.

save() in the mongo shell

The save() helper method in the mongo shell provides a shorthand syntax to perform an update of a single document with upsert semantics:

 

> // x is some JSON style object
> db.mycollection.save(x); // updates if exists; inserts if new
> 
> // equivalent to:
> db.mycollection.update( { _id: x._id }, x, /*upsert*/ true );
 

Modifier Operations

Modifier operations are highly-efficient and useful when updating existing values; for instance, they're great for incrementing a number.

So, while a conventional implementation does work:

var j=myColl.findOne( { name: "Joe" } );
j.n++;
myColl.save(j);
 

 

a modifier update has the advantages of avoiding the latency involved in querying and returning the object as well as resulting in very little network data transfer. The modifier update is also also atomic (per document that is; on a multi-document update, there is effectively an auto-commit after each individual document update).

You specify any of the special update operators (which always start with a '$' character) with a relevant update document:

db.people.update( { name:"Joe" }, { $inc: { n : 1 } } );
$inc
{ $inc : { field : value } }

 increments field by the number value if field is present in the object, otherwise sets field to the number value . This can also be used to decrement by using a negative value .

$set
{ $set : { field : value } }

 sets field to value . All datatypes are supported with $set .

$push
{ $push : { field : value } }
 

appends value to field , if field is an existing array, otherwise sets field to the array [value ] if field is not present. If field is present but is not an array, an error condition is raised.

Multiple arrays may be updated in one operation by comma separating the field : value pairs:

{ $push : { field : value, field2 : value2 } }
$pop
{ $pop : { field : 1  } }

 removes the last element in an array (ADDED in 1.1)

The $ positional operator

The $ operator (by itself) means "position of the matched array item in the query". Use this to find an array member and then manipulate it. For example:

 

> t.find()
{ "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
  "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }

> t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )

> t.find()
{ "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
  "comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }

 

参考:

http://www.mongodb.org/display/DOCS/Updating#Updating-%24inc

http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值