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 }
将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 }
相关文章推荐
- mongo-update 操作(1)
- mongo-update 操作(2)
- 3.数据库操作相关术语,Oracle认证,insert into,批量插入,update tablename set,delete和truncate的差别,sql文件导入
- MongoDB update操作文档
- 使用JPA中@Query 注解实现update 操作
- Oracle 大规模 delete,update 操作 注意事项
- 数据库Update操作的一些技巧总结
- 使用脚本操作UpdatePanel中控件的问题
- Access update语句 提示 "操作必须使用一个可更新的查询"Access update语句 提示 "操作必须使用一个可更新的查询"
- updatepanel控件中弹出子窗口,子窗口操作成功后自动关闭并刷新父页面
- sqlserver update或者Delete误操作恢复方法---【验证】
- oracle update、delete或者drop误操作如何恢复之前是数据
- SQL 游标使用的小例子,对于每行数据根据参数的不同进行Insert或Update操作.
- thinkphp下mongo的操作
- Mongodb的update操作
- mysql关联多表进行update更新操作
- Oracle和SQL Server的对比 - 连接表的更新操作Update SQL 语句
- 用UPDATE针对多表操作
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- java mongo操作