update() 方法用于更新已存在的文档。语法格式如下:
db.集合名.update(query,update[,upsert,multi])
参数:
query:必要参数,代表原有的数据,可使用如下参数指定条件范围:
update:必要参数,代表更新后的数据,可使用如下参数指定修改方式:
upsert:可选参数,如果不存在update记录,是否插入更新的数据,是一个布尔值
multi:可选参数,是否将查询出来的数据全部更新,是一个布尔值
db.student.insert([
{"name":"zs","age":20,"class":1},
{"name":"aaa","age":21,"class":1},
{"name":"bbb","age":20,"class":2},
{"name":"ccc","age":21,"class":1},
{"name":"ddd","age":21,"class":2}])
将name=‘zs’的数据改为’000’
> db.student.update({"name":"zs"},{"name":"000"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("6239db19f77d1e577d985cc3"), "name" : "000" }
{ "_id" : ObjectId("6239db19f77d1e577d985cc4"), "name" : "aaa", "age" : 21, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc5"), "name" : "bbb", "age" : 20, "class" : 2 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc6"), "name" : "ccc", "age" : 21, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc7"), "name" : "ddd", "age" : 21, "class" : 2 }
使用这种方式传参会改变整个数据,想要只修改一项可使用下面的方法
> db.student.update({"name":"zs"},{"$set":{"name":"000"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("6239db19f77d1e577d985cc3"), "name" : "000", "age" : 20, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc4"), "name" : "aaa", "age" : 21, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc5"), "name" : "bbb", "age" : 20, "class" : 2 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc6"), "name" : "ccc", "age" : 21, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc7"), "name" : "ddd", "age" : 21, "class" : 2 }
把年龄大于20的人放入0班
> db.student.update({"age":{"$gt":20}},{"$set":{"class":0}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.student.find()
{ "_id" : ObjectId("6239db19f77d1e577d985cc3"), "name" : "000", "age" : 20, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc4"), "name" : "aaa", "age" : 21, "class" : 0 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc5"), "name" : "bbb", "age" : 20, "class" : 2 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc6"), "name" : "ccc", "age" : 21, "class" : 0 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc7"), "name" : "ddd", "age" : 21, "class" : 0 }
使用multi:true
来改变所有满足条件的数据
> db.student.update({"name":"000"},{"$unset":{"name":"000"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("6239db19f77d1e577d985cc3"), "age" : 20, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc4"), "name" : "aaa", "age" : 21, "class" : 0 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc5"), "name" : "bbb", "age" : 20, "class" : 2 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc6"), "name" : "ccc", "age" : 21, "class" : 0 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc7"), "name" : "ddd", "age" : 21, "class" : 0 }
把aaa的索引name
改为newname
> db.student.update({"name":"aaa"},{"$rename":{"name":"newname"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("6239db19f77d1e577d985cc3"), "age" : 20, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc4"), "age" : 21, "class" : 0, "newname" : "aaa" }
{ "_id" : ObjectId("6239db19f77d1e577d985cc5"), "name" : "bbb", "age" : 20, "class" : 2 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc6"), "name" : "ccc", "age" : 21, "class" : 0 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc7"), "name" : "ddd", "age" : 21, "class" : 0 }
让bbb的age加2
> db.student.update({"name":"bbb"},{$inc:{"age":2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("6239db19f77d1e577d985cc3"), "age" : 20, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc4"), "age" : 21, "class" : 0, "newname" : "aaa" }
{ "_id" : ObjectId("6239db19f77d1e577d985cc5"), "name" : "bbb", "age" : 22, "class" : 2 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc6"), "name" : "ccc", "age" : 21, "class" : 0 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc7"), "name" : "ddd", "age" : 21, "class" : 0 }
save() 方法通过传入的文档来替换已有文档,_id 主键存在就更新,不存在就插入。
把zs修改回来
> db.student.save({"_id" : ObjectId("6239db19f77d1e577d985cc3"), "name" : "zs", "age" : 20, "class" : 1 })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("6239db19f77d1e577d985cc3"), "name" : "zs", "age" : 20, "class" : 1 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc4"), "age" : 21, "class" : 0, "newname" : "aaa" }
{ "_id" : ObjectId("6239db19f77d1e577d985cc5"), "name" : "bbb", "age" : 22, "class" : 2 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc6"), "name" : "ccc", "age" : 21, "class" : 0 }
{ "_id" : ObjectId("6239db19f77d1e577d985cc7"), "name" : "ddd", "age" : 21, "class" : 0 }