因为MongoDB 复杂的数据结构, 所以查询修改的语法要比RDBMS 的单表操作要复杂一些。 下面记录一些常见的Update 操作。
db.collection.update(, , { upsert: , multi: })
在MongoDB 2.6, 又多支持了一个选项 writeConcern: <document>。
- <query> 决定什么样的document 会被更新
- <update> 决定符合条件的document 会被更新成什么样
- option upsert: 默认为false, 如果设为true, 如果<query> 没有match 里面的任何操作,这个update 会变成insert
- option multi: 默认为false, 即使<query> 里面有很多记录,也只会更新第一行。
常用原型:
db.table.update(<query>, <document>), 符合query 条件的第一条整条记录会被<document> 代替, <document> 没有使用$set 操作符号。
mongolog:PRIMARY> db.test.insert({"name":"name1", "params": {"p1":"v1", "p2": "v2"}, "type": "type1"})WriteResult({ "nInserted" : 1 })mongolog:PRIMARY> db.test.findOne(){ "_id" : ObjectId("5456e188bc391a4f85e4aa4f"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "v2" }, "type" : "type1"}mongolog:PRIMARY> db.test.update({"name":"name1"}, {"type":"type2"})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })mongolog:PRIMARY> db.test.findOne(){ "_id" : ObjectId("5456e051bc391a4f85e4aa4e"), "type" : "type2" } // 只剩下了type
db.table.update(<query>, {$set: {"key": "document"}}). 仅仅key 对应的document 会被document 代替, 如果对应的Key 没有,则添加这个key.
- 如果只想修改param 的值,其他列的值保持不变:
mongolog:PRIMARY> db.test.update({"name":"name1"}, {$set: {"params": {"p2":"newV2"}}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })mongolog:PRIMARY> db.test.findOne(){ "_id" : ObjectId("5456e406bc391a4f85e4aa51"), "name" : "name1", "params" : { "p2" : "newV2" // Params 中只剩下p2 }, "type" : "type1"}
- 如果想只更新里面params.p2 的值,则需要
mongolog:PRIMARY> db.test.update({"name": "name1"}, {$set: {"params.p2":"newV2"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })mongolog:PRIMARY> db.test.findOne(){ "_id" : ObjectId("5456e267bc391a4f85e4aa50"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "newV2" // 只有p2 的值被更改 }, "type" : "type1"}
- 如果想增加一个params.p3, 只需要:
mongolog:PRIMARY> db.test.update({"name": "name1"}, {$set: {"params.p3":"v3"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })mongolog:PRIMARY> db.test.findOne(){ "_id" : ObjectId("5456e267bc391a4f85e4aa50"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "newV2", "p3" : "v3" }, "type" : "type1"}
db.table.update(<query>, <document>, { multi: true } ) 修改所有的符合条件的document。
mongolog:PRIMARY> db.test.update({"name":"name1"}, {"$set": {"params.p4":"v4"}}) // 没有设置{mutli:true}, 只更新了一行WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })mongolog:PRIMARY> db.test.find(){ "_id" : ObjectId("5456e406bc391a4f85e4aa51"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "v2" }, "type" : "type1" }{ "_id" : ObjectId("5456e267bc391a4f85e4aa50"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "newV2", "p3" : "v3", "p4" : "v4" }, "type" : "type1" }mongolog:PRIMARY> db.test.update({"name":"name1"}, {"$set": {"params.p5":"v5"}}, {multi:true})WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })mongolog:PRIMARY> db.test.find(){ "_id" : ObjectId("5456e406bc391a4f85e4aa51"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "v2", "p5" : "v5" }, "type" : "type1" }{ "_id" : ObjectId("5456e267bc391a4f85e4aa50"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "newV2", "p3" : "v3", "p4" : "v4", "p5" : "v5" }, "type" : "type1" }
db.table.update(<query>, <document>, {"upsert": true}) 如果没有matched 的文档,就会新insert 一行
mongolog:PRIMARY> db.test.update({"name":"name2"}, {$set: {"params.p2": "v22"}}, {"upsert": true})WriteResult({"nMatched" : 0,"nUpserted" : 1,"nModified" : 0,"_id" : ObjectId("5456e580c71b89fa66bff2f5")})mongolog:PRIMARY> db.test.find(){ "_id" : ObjectId("5456e406bc391a4f85e4aa51"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "v2", "p5" : "v5" }, "type" : "type1" }{ "_id" : ObjectId("5456e267bc391a4f85e4aa50"), "name" : "name1", "params" : { "p1" : "v1", "p2" : "newV2", "p3" : "v3", "p4" : "v4", "p5" : "v5" }, "type" : "type1" }{ "_id" : ObjectId("5456e580c71b89fa66bff2f5"), "name" : "name2", "params" : { "p2" : "v22" } } db.parameter.update({}, { $unset: {"hareware": ""}}, {multi:true})