您的位置:首页 > 编程语言 > Go语言

mongo-update 操作(3)

2013-02-04 10:44 190 查看
1. upsert

将update的第三个参数设置为true,存在则更新,不存在则插入。

> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
> db.tianyc03.update({count:20},{$inc:{count:3}}, true)
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 23 }
> db.tianyc03.update({count:20},{$inc:{count:3}}, true)
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 23 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }

> db.tianyc03.update({count:23},{$inc:{count:3}}, true)

> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
>

2. save

若文档中不包含_id,save和insert功能相同;若包含_id,则save和upsert功能相同。

> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
> db.tianyc03.save({count:24})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
{ "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 24 }

> db.tianyc03.save({count:24})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
{ "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 24 }
{ "_id" : ObjectId("510f1c8f8474cd8af024e2d1"),"count" : 24 }

> db.tianyc03.save({"_id" : ObjectId("510f1b718474cd8af024e2d0"),count:244})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
{ "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 244 }

{ "_id" : ObjectId("510f1c8f8474cd8af024e2d1"),"count" : 24 }

#而insert会提示插入失败。
> db.tianyc03.insert({"_id" : ObjectId("510f1b718474cd8af024e2d0"),count:244})
E11000 duplicate key error index: test.tianyc03.$_id_ dup key: { : ObjectId('510f1b718474cd8af024e2d0') }

3. 批量更新

默认情况下,update只更新匹配到的第一行记录。将update的第四个参数设置为true,则批量更新搜索到的所有记录。

> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "count" : 11 }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
{ "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 244 }
{ "_id" : ObjectId("510f1c8f8474cd8af024e2d1"), "count" : 24 }
#将所有count值增加10,但却只增加了第一行。

> db.tianyc03.update({},{$inc:{count:10}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "count" : 21 }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
{ "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 244 }
{ "_id" : ObjectId("510f1c8f8474cd8af024e2d1"), "count" : 24 }

#再增加一次,还是只增加了第一行。

> db.tianyc03.update({},{$inc:{count:10}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "count" : 31 }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
{ "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 244 }
{ "_id" : ObjectId("510f1c8f8474cd8af024e2d1"), "count" : 24 }

#使用批量更新,达到目的。

> db.tianyc03.update({},{$inc:{count:10}},false,true)
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "count" : 41 }
{ "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 36 }
{ "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 33 }
{ "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 254 }
{ "_id" : ObjectId("510f1c8f8474cd8af024e2d1"), "count" : 34 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: