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

猜你喜欢

转载自san-yun.iteye.com/blog/1720305