【MongoDB】修改文档/更新文档

修改、更新文档

  • update() 方法
    • 添加数据
    • 修改指定数据
    • 修改满足范围的数据
    • 删除指定数据值
    • 修改索引名
    • 对原数据进行增减操作
  • save()方法

update() 方法

update() 方法用于更新已存在的文档。语法格式如下:

db.集合名.update(query,update[,upsert,multi])

参数:
query:必要参数,代表原有的数据,可使用如下参数指定条件范围:

  • 大于:$gt
  • 小于:$lt
  • 等于:$eq
  • 不等于:$ne
  • 大于等于:$gte
  • 小于等于:$lte

update:必要参数,代表更新后的数据,可使用如下参数指定修改方式:

  • $set:设置值
  • $rename:重命名
  • $unset:删除
  • $inc:增加值

upsert:可选参数,如果不存在update记录,是否插入更新的数据,是一个布尔值

  • true:插入
  • false:不插入(默认)

multi:可选参数,是否将查询出来的数据全部更新,是一个布尔值

  • true:代表全部更新
  • false:代表只更新一条(默认)

添加数据

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()方法

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 }

你可能感兴趣的:(MongoDB,mongodb,数据库)