db.collection.update( criteria, objNew, upsert, multi )
参数说明:
Criteria:用于设置查询条件的对象
Objnew:用于设置更新内容的对象
Upsert:如果记录已经存在,更新它,否则新增一个记录
Multi:如果有多个符合条件的记录,全部更新
注意:默认情况下,只会更新第一个符合条件的记录
$set
功能:把 field 的值设置成 value,当 field 不存在时,增加一个字段,类似 SQL 的 set 操作,value 支持所有类型
> db.teachers.find()
{ "_id" : ObjectId("5500f4031c8361936a1e6a08"), "name" : "Mr A", "age" : 40, "graduates" : [ { "name" : "Zhang", "age" : 22 }, { "name" : "Yao", "age" : 23, "phone" : [ "13100001111" ] }, { "name" : "Chen", "age" : 24, "gender" : "male" } ] }
{ "_id" : ObjectId("5500f45a1c8361936a1e6a09"), "name" : "Mr B", "age" : 45, "graduates" : [ { "name" : "Hou", "age" : 30 }, { "name" : "Liu", "age" : 23, "phone" : [ "13200001111" ] }, { "name" : "Ding", "age" : 25, "gender" : "female" } ] }
{ "_id" : ObjectId("5500f49e1c8361936a1e6a0a"), "name" : "Mr B", "age" : 45, "graduates" : [ { "name" : "Qian", "age" : 19 }, { "name" : "Ma", "age" : 22, "phone" : [ "13300001111" ] } ] }
{ "_id" : ObjectId("5500f4bf1c8361936a1e6a0b"), "name" : "Mr C", "age" : 30, "graduates" : [ { } ] }
{ "_id" : ObjectId("5500f4dd1c8361936a1e6a0c"), "name" : "Mr D", "age" : 65, "graduates" : [ ] }
{ "_id" : ObjectId("5500f4f11c8361936a1e6a0d"), "name" : "Mr E" }
{ "_id" : ObjectId("5500fef91c8361936a1e6a0e"), "name" : "Mrs F", "age" : 43, "graduates" : [ { "name" : "Han", "age" : 23 } ] }
>
> db.teachers.update({name:"Mr B"},{$set:{age:46}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.teachers.find()
{ "_id" : ObjectId("5500f4031c8361936a1e6a08"), "name" : "Mr A", "age" : 40, "graduates" : [ { "name" : "Zhang", "age" : 22 }, { "name" : "Yao", "age" : 23, "phone" : [ "13100001111" ] }, { "name" : "Chen", "age" : 24, "gender" : "male" } ] }
{ "_id" : ObjectId("5500f45a1c8361936a1e6a09"), "name" : "Mr B", "age" : 46, "graduates" : [ { "name" : "Hou", "age" : 30 }, { "name" : "Liu", "age" : 23, "phone" : [ "13200001111" ] }, { "name" : "Ding", "age" : 25, "gender" : "female" } ] }
{ "_id" : ObjectId("5500f49e1c8361936a1e6a0a"), "name" : "Mr B", "age" : 45, "graduates" : [ { "name" : "Qian", "age" : 19 }, { "name" : "Ma", "age" : 22, "phone" : [ "13300001111" ] } ] }
{ "_id" : ObjectId("5500f4bf1c8361936a1e6a0b"), "name" : "Mr C", "age" : 30, "graduates" : [ { } ] }
{ "_id" : ObjectId("5500f4dd1c8361936a1e6a0c"), "name" : "Mr D", "age" : 65, "graduates" : [ ] }
{ "_id" : ObjectId("5500f4f11c8361936a1e6a0d"), "name" : "Mr E" }
{ "_id" : ObjectId("5500fef91c8361936a1e6a0e"), "name" : "Mrs F", "age" : 43, "graduates" : [ { "name" : "Han", "age" : 23 } ] }
>
> db.teachers.update({name:"Mr B"},{$set:{age:47}},0,1)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.teachers.find()
{ "_id" : ObjectId("5500f4031c8361936a1e6a08"), "name" : "Mr A", "age" : 40, "graduates" : [ { "name" : "Zhang", "age" : 22 }, { "name" : "Yao", "age" : 23, "phone" : [ "13100001111" ] }, { "name" : "Chen", "age" : 24, "gender" : "male" } ] }
{ "_id" : ObjectId("5500f45a1c8361936a1e6a09"), "name" : "Mr B", "age" : 47, "graduates" : [ { "name" : "Hou", "age" : 30 }, { "name" : "Liu", "age" : 23, "phone" : [ "13200001111" ] }, { "name" : "Ding", "age" : 25, "gender" : "female" } ] }
{ "_id" : ObjectId("5500f49e1c8361936a1e6a0a"), "name" : "Mr B", "age" : 47, "graduates" : [ { "name" : "Qian", "age" : 19 }, { "name" : "Ma", "age" : 22, "phone" : [ "13300001111" ] } ] }
{ "_id" : ObjectId("5500f4bf1c8361936a1e6a0b"), "name" : "Mr C", "age" : 30, "graduates" : [ { } ] }
{ "_id" : ObjectId("5500f4dd1c8361936a1e6a0c"), "name" : "Mr D", "age" : 65, "graduates" : [ ] }
{ "_id" : ObjectId("5500f4f11c8361936a1e6a0d"), "name" : "Mr E" }
{ "_id" : ObjectId("5500fef91c8361936a1e6a0e"), "name" : "Mrs F", "age" : 43, "graduates" : [ { "name" : "Han", "age" : 23 } ] }
>
$inc
功能:把value 的值加一个数
> db.teachers.update({name:"Mr B"},{$inc:{age:1}},0,1)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.teachers.find()
{ "_id" : ObjectId("5500f4031c8361936a1e6a08"), "name" : "Mr A", "age" : 40, "graduates" : [ { "name" : "Zhang", "age" : 22 }, { "name" : "Yao", "age" : 23, "phone" : [ "13100001111" ] }, { "name" : "Chen", "age" : 24, "gender" : "male" } ] }
{ "_id" : ObjectId("5500f45a1c8361936a1e6a09"), "name" : "Mr B", "age" : 48, "graduates" : [ { "name" : "Hou", "age" : 30 }, { "name" : "Liu", "age" : 23, "phone" : [ "13200001111" ] }, { "name" : "Ding", "age" : 25, "gender" : "female" } ] }
{ "_id" : ObjectId("5500f49e1c8361936a1e6a0a"), "name" : "Mr B", "age" : 48, "graduates" : [ { "name" : "Qian", "age" : 19 }, { "name" : "Ma", "age" : 22, "phone" : [ "13300001111" ] } ] }
{ "_id" : ObjectId("5500f4bf1c8361936a1e6a0b"), "name" : "Mr C", "age" : 30, "graduates" : [ { } ] }
{ "_id" : ObjectId("5500f4dd1c8361936a1e6a0c"), "name" : "Mr D", "age" : 65, "graduates" : [ ] }
{ "_id" : ObjectId("5500f4f11c8361936a1e6a0d"), "name" : "Mr E" }
{ "_id" : ObjectId("5500fef91c8361936a1e6a0e"), "name" : "Mrs F", "age" : 43, "graduates" : [ { "name" : "Han", "age" : 23 } ] }
>
增加一条Mr G的文档。但是没想到age将name字段直接覆盖了!!
> db.teachers.update({name:"Mr G"},{age:34},1)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("55015736bc0dfd695316ad51")
})
> db.teachers.find()
{ "_id" : ObjectId("5500f4031c8361936a1e6a08"), "name" : "Mr A", "age" : 40, "graduates" : [ { "name" : "Zhang", "age" : 22 }, { "name" : "Yao", "age" : 23, "phone" : [ "13100001111" ] }, { "name" : "Chen", "age" : 24, "gender" : "male" } ] }
{ "_id" : ObjectId("5500f45a1c8361936a1e6a09"), "name" : "Mr B", "age" : 48, "graduates" : [ { "name" : "Hou", "age" : 30 }, { "name" : "Liu", "age" : 23, "phone" : [ "13200001111" ] }, { "name" : "Ding", "age" : 25, "gender" : "female" } ] }
{ "_id" : ObjectId("5500f49e1c8361936a1e6a0a"), "name" : "Mr B", "age" : 48, "graduates" : [ { "name" : "Qian", "age" : 19 }, { "name" : "Ma", "age" : 22, "phone" : [ "13300001111" ] } ] }
{ "_id" : ObjectId("5500f4bf1c8361936a1e6a0b"), "name" : "Mr C", "age" : 30, "graduates" : [ { } ] }
{ "_id" : ObjectId("5500f4dd1c8361936a1e6a0c"), "name" : "Mr D", "age" : 65, "graduates" : [ ] }
{ "_id" : ObjectId("5500f4f11c8361936a1e6a0d"), "name" : "Mr E" }
{ "_id" : ObjectId("5500fef91c8361936a1e6a0e"), "name" : "Mrs F", "age" : 43, "graduates" : [ { "name" : "Han", "age" : 23 } ] }
{ "_id" : ObjectId("55015736bc0dfd695316ad51"), "age" : 34 }
>
所以,如果需要再原文档基础上进行修改,需要使用$set。
> db.teachers.update({_id:ObjectId("55015736bc0dfd695316ad51")},{$set:{name:"Mr G",gender:"male"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.teachers.find()
{ "_id" : ObjectId("5500f4031c8361936a1e6a08"), "name" : "Mr A", "age" : 40, "graduates" : [ { "name" : "Zhang", "age" : 22 }, { "name" : "Yao", "age" : 23, "phone" : [ "13100001111" ] }, { "name" : "Chen", "age" : 24, "gender" : "male" } ] }
{ "_id" : ObjectId("5500f45a1c8361936a1e6a09"), "name" : "Mr B", "age" : 48, "graduates" : [ { "name" : "Hou", "age" : 30 }, { "name" : "Liu", "age" : 23, "phone" : [ "13200001111" ] }, { "name" : "Ding", "age" : 25, "gender" : "female" } ] }
{ "_id" : ObjectId("5500f49e1c8361936a1e6a0a"), "name" : "Mr B", "age" : 48, "graduates" : [ { "name" : "Qian", "age" : 19 }, { "name" : "Ma", "age" : 22, "phone" : [ "13300001111" ] } ] }
{ "_id" : ObjectId("5500f4bf1c8361936a1e6a0b"), "name" : "Mr C", "age" : 30, "graduates" : [ { } ] }
{ "_id" : ObjectId("5500f4dd1c8361936a1e6a0c"), "name" : "Mr D", "age" : 65, "graduates" : [ ] }
{ "_id" : ObjectId("5500f4f11c8361936a1e6a0d"), "name" : "Mr E" }
{ "_id" : ObjectId("5500fef91c8361936a1e6a0e"), "name" : "Mrs F", "age" : 43, "graduates" : [ { "name" : "Han", "age" : 23 } ] }
{ "_id" : ObjectId("55015736bc0dfd695316ad51"), "age" : 34, "name" : "Mr G", "gender" : "male" }
>
$unset
功能:删除给定的字段 field,从MongoDB 1.3 版本以后支持
> db.teachers.update({name:"Mr G"},{$unset:{gender:1}},0,1)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.teachers.find()
{ "_id" : ObjectId("5500f4031c8361936a1e6a08"), "name" : "Mr A", "age" : 40, "graduates" : [ { "name" : "Zhang", "age" : 22 }, { "name" : "Yao", "age" : 23, "phone" : [ "13100001111" ] }, { "name" : "Chen", "age" : 24, "gender" : "male" } ] }
{ "_id" : ObjectId("5500f45a1c8361936a1e6a09"), "name" : "Mr B", "age" : 48, "graduates" : [ { "name" : "Hou", "age" : 30 }, { "name" : "Liu", "age" : 23, "phone" : [ "13200001111" ] }, { "name" : "Ding", "age" : 25, "gender" : "female" } ] }
{ "_id" : ObjectId("5500f49e1c8361936a1e6a0a"), "name" : "Mr B", "age" : 48, "graduates" : [ { "name" : "Qian", "age" : 19 }, { "name" : "Ma", "age" : 22, "phone" : [ "13300001111" ] } ] }
{ "_id" : ObjectId("5500f4bf1c8361936a1e6a0b"), "name" : "Mr C", "age" : 30, "graduates" : [ { } ] }
{ "_id" : ObjectId("5500f4dd1c8361936a1e6a0c"), "name" : "Mr D", "age" : 65, "graduates" : [ ] }
{ "_id" : ObjectId("5500f4f11c8361936a1e6a0d"), "name" : "Mr E" }
{ "_id" : ObjectId("5500fef91c8361936a1e6a0e"), "name" : "Mrs F", "age" : 43, "graduates" : [ { "name" : "Han", "age" : 23 } ] }
{ "_id" : ObjectId("55015736bc0dfd695316ad51"), "age" : 34, "name" : "Mr G" }
>
这里需要注意的时,$set等用于修改keyvalue的魔术方法一般都是外侧方法。
之前用于查询的魔术方法一般都是内侧方法。当然,像逻辑条件“$or”魔术方法等则比较特殊,是外侧方法并且可以嵌套。