英文原文:http://www.mongodb.org/display/DOCS/Updating
mongodb支持原子的更新操作,对文档进行更新替换旧的文档或者更新文档中某个字段。
update操作用新的文档对象更新匹配的旧文档,如果仅希望更新文档中某些字段的值可以使用 “$"操作符,像后面提到的。
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 - if all documents matching criteria should be updated
upsert 选项为真的时候,做更新操作或插入操作,即如果存在则更新,否则直接插入文档。
save()操作相当于update()操作在选项upsert为true的情况下,如下:
// x is some JSON style object
db.mycollection.save(x); // updates if exists; inserts if new
呵呵,今天就到这了,有时间再补充