[MongoDB]Update更新数据

Posted @SmartSi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[MongoDB]Update更新数据相关的知识,希望对你有一定的参考价值。

Update操作只作用于集合中存在的文档。MongoDB提供了如下方法来更新集合中的文档:

  • db.collection.update()
  • db.collection.updateOne() New in version 3.2
  • db.collection.updateMany() New in version 3.2
  • db.collection.replaceOne() New in version 3.2

你可以通过指定criteria或者filter来指定你想更新的文档:

update函数执行数据更新操作,该函数接受3个主要参数:criteria,action,options:

  • 参数criteria用于指定一个查询,查询选择将要更新的目标记录。
  • 参数action用于指定更新信息,也可以使用操作符来完成。
  • 参数options用于指定更新文档时的选项,可选值包括:upsert和multi。upsert可以指定如果数据存在就更新,不存在就创建数据;multi选项指定是否应该更新所有匹配的文档,或者只更新第一个文档(默认行为)。

为了更好的演示,插入数据:

db.users.insertMany(
   [
     
       _id: 1,
       name: "sue",
       age: 19,
       type: 1,
       status: "P",
       favorites:  artist: "Picasso", food: "pizza" ,
       finished: [ 17, 3 ],
       badges: [ "blue", "black" ],
       points: [
           points: 85, bonus: 20 ,
           points: 85, bonus: 10 
       ]
     ,
     
       _id: 2,
       name: "bob",
       age: 42,
       type: 1,
       status: "A",
       favorites:  artist: "Miro", food: "meringue" ,
       finished: [ 11, 25 ],
       badges: [ "green" ],
       points: [
           points: 85, bonus: 20 ,
           points: 64, bonus: 12 
       ]
     ,
     
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites:  artist: "Cassatt", food: "cake" ,
       finished: [ 6 ],
       badges: [ "blue", "Picasso" ],
       points: [
           points: 81, bonus: 8 ,
           points: 55, bonus: 20 
       ]
     ,
     
       _id: 4,
       name: "xi",
       age: 34,
       type: 2,
       status: "D",
       favorites:  artist: "Chagall", food: "chocolate" ,
       finished: [ 5, 11 ],
       badges: [ "Picasso", "black" ],
       points: [
           points: 53, bonus: 15 ,
           points: 51, bonus: 15 
       ]
     ,
     
       _id: 5,
       name: "xyz",
       age: 23,
       type: 2,
       status: "D",
       favorites:  artist: "Noguchi", food: "nougat" ,
       finished: [ 14, 6 ],
       badges: [ "orange" ],
       points: [
           points: 71, bonus: 20 
       ]
     ,
     
       _id: 6,
       name: "abc",
       age: 43,
       type: 1,
       status: "A",
       favorites:  food: "pizza", artist: "Picasso" ,
       finished: [ 18, 12 ],
       badges: [ "black", "blue" ],
       points: [
           points: 78, bonus: 8 ,
           points: 57, bonus: 7 
       ]
     
   ]
)

1. 字段更新操作符

1.1 覆盖更新

下面的例子使用update()函数执行更新数据操作,不包含操作符:

> db.users.find("name":"xyz");
 "_id" : 5, "name" : "xyz", "age" : 23, "type" : 2, "status" : "D", "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 14, 6 ], "badges" : [ "orange" ], "points" : [  "points" : 71, "bonus" : 20  ] 
>

修改之后:

> db.users.update("name":"xyz", name : "xyz", age:25, school : "xidian", type:1,  favorites: artist : "Noguchi", food : "nougat", finished : [4, 5] , upsert:true);
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> 
> 
> 
> db.users.find("name":"xyz");
 "_id" : 5, "name" : "xyz", "age" : 25, "school" : "xidian", "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 5 ] 

该例覆写了集合中的文档,并保存更新后的值。

备注

任何忽略的字段都被移除(文档被覆盖)

1.2 upsert

upsert可以指定如果数据存在就更新,不存在就创建数据。

> db.users.find("name":"yoona");
> 
> db.users.update("name":"yoona", name : "yoona", age:25, "school" : "xidian", type:1,  favorites: artist : "Noguchi", food : "nougat", finished : [4, 5] , upsert:false);
WriteResult( "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 )

更新操作之前我们没有查询到yoona的任何信息,然后我们对其进行更新操作,并且upsert设置为false,表示如果更新的数据不存在,不走任何操作。更新之后,我们再次查询一下:

> db.users.find("name":"yoona");
>

再次查询还是没有找到相应数据。我们设置upsert为true,表示如果数据存在则更新,如果不存在则创建该数据:

> db.users.update("name":"yoona", name : "yoona", age:25, "school" : "xidian", type:1,  favorites: artist : "Noguchi", food : "nougat", finished : [4, 5] , upsert:true);
WriteResult(
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("57c3ad26d2cc0133a95bc583")
)
> 
> db.users.find("name":"yoona");
 "_id" : ObjectId("57c3ad26d2cc0133a95bc583"), "name" : "yoona", "age" : 25, "school" : "xidian", "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 5 ] 

1.3 $inc 增加值

操作符$inc可以为指定的键执行(原子)更新操作,如果字段存在,就将该值增加给定的增量,如果该字段不存在,就创建该字段。

> db.users.update("name":"yoona", $inc:age:2);
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> 
> 
> db.users.find(name:"yoona");
 "_id" : ObjectId("57c3ad26d2cc0133a95bc583"), "name" : "yoona", "age" : 27, "school" : "xidian", "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 5 ] 

上面例子中将yoona用户的年龄增加两岁。

1.4 $set 设置字段值

可以使用$set操作符将某个字段设置为指定值。

> db.users.find(name:"yoona");
 "_id" : ObjectId("57c3ad26d2cc0133a95bc583"), "name" : "yoona", "age" : 27, "school" : "xidian", "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 5 ] 
> 
> db.users.update("name":"yoona", $set:school:"Massachusetts Institute of Technology");
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> 
> db.users.find(name:"yoona");
 "_id" : ObjectId("57c3ad26d2cc0133a95bc583"), "name" : "yoona", "age" : 27, "school" : "Massachusetts Institute of Technology", "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 5 ] 
>

上面例子将yoona用户的学校改为麻省理工学院。

1.5 $unset删除指定字段

> db.users.find(name:"yoona");
 "_id" : ObjectId("57c3ad26d2cc0133a95bc583"), "name" : "yoona", "age" : 27, "school" : "Massachusetts Institute of Technology", "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 5 ] 
> 
> db.users.update("name":"yoona", $unset:school:"Massachusetts Institute of Technology");
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> 
> db.users.find(name:"yoona");
 "_id" : ObjectId("57c3ad26d2cc0133a95bc583"), "name" : "yoona", "age" : 27, "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 5 ] 

上面例子将yoona用户的学校删除。

1.6 $rename 重命名字段名称

格式:

$rename:  <field1>: <newName1>, <field2>: <newName2>, ...  

新字段名称必须不同与已经存在的字段名称

> db.users.find(name:"yoona");
 "_id" : ObjectId("57c3ad26d2cc0133a95bc583"), "name" : "yoona", "age" : 27, "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 5 ] 
> 
> db.users.update("name":"yoona", $rename: name:"userName", age:"userAge");
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
>

查询:

> db.users.find(name:"yoona");
> db.users.find(userName:"yoona");
 "_id" : ObjectId("57c3ad26d2cc0133a95bc583"), "type" : 1, "favorites" :  "artist" : "Noguchi", "food" : "nougat" , "finished" : [ 4, 以上是关于[MongoDB]Update更新数据的主要内容,如果未能解决你的问题,请参考以下文章

Mongodb数据更新命令(updatesave)

[MongoDB]Update更新数据

mongodb的update函数更新数据,更新文档中的某个具体字段的数据

mongoDB 更新数据

MongoDB(课时17 更新函数)

Update_one 或 insert_many 更新 mongodb 中的记录?