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 }