MongoDB 更新文档

更新文档

MongoDB的update()save()方法用于将文档更新为集合。update()方法更新现有文档中的值,而save()方法将现有文档替换为save()方法中传递的文档。

update()方法

update()方法更新现有文档中的值。
语法

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

示例
考虑到mycol集合具有以下数据。

{ "_id" : ObjectId("5f4871eb69ad8645590791a2"), "title" : "MongoDB Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a3"), "title" : "NoSQL Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a4"), "title" : "Tutorials Point Overview" }

以下示例将为标题为“MongoDB Overview”的文档设置新标题“New MongoDB Tutorial”。

db.mycol.find();  # 查询现有数据
{ "_id" : ObjectId("5f4871eb69ad8645590791a2"), "title" : "MongoDB Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a3"), "title" : "NoSQL Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a4"), "title" : "Tutorials Point Overview" }
> db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}}) # 修改
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })  #修改返回
> db.mycol.find(); #查询修改后的数据
{ "_id" : ObjectId("5f4871eb69ad8645590791a2"), "title" : "New MongoDB Tutorial" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a3"), "title" : "NoSQL Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a4"), "title" : "Tutorials Point Overview" }

默认情况下,MongoDB仅更新一个文档。要更新多个文档,您需要将参数“multi”设置为true。-

db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})

save() 方法

save()方法通过了新的文件替换现有的文档(带_id参数且存在该记录,如果不存在则插入一条新的)。
语法

>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

以下示例从名为mycol的集合中检索所有文档,并以易于阅读的格式排列它们。

> db.mycol.find() # 现有数据
{ "_id" : ObjectId("5f4871eb69ad8645590791a2"), "title" : "New MongoDB Tutorial" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a3"), "title" : "NoSQL Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a4"), "title" : "Tutorials Point Overview" }
> db.mycol.save( { "_id" : ObjectId("5f4871eb69ad8645590791a2"),  "title":"Tutorials Point New Topic Good  good", "by":"蝴蝶教程" } );  # save 替换_id 为 5f4871eb69ad8645590791a2的文档
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })  # save 执行返回
> db.mycol.find() #查询修改后的数据
{ "_id" : ObjectId("5f4871eb69ad8645590791a2"), "title" : "Tutorials Point New Topic Good  good", "by" : "蝴蝶教程" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a3"), "title" : "NoSQL Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a4"), "title" : "Tutorials Point Overview" }

findOneAndUpdate() 方法

findOneAndUpdate() 方法中的现有文档中更新的值。
语法:

>db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA)

假设我们创建了一个名为empDetails的集合,并在其中插入了三个文档,如下所示-

db.empDetails.insertMany(
[
{
title: "Java 教程",
author: "张三",
phone: "18812454545"
},
{
title: "PHP 教程",
author: "李四",
phone: "18912454545"
},
{
title: "Python 教程",
author: "王五",
phone: "19912454545"
}
]
)

以下示例更新author为“李四”的文档的phone值。

db.empDetails.find(); #修改前
{ "_id" : ObjectId("5f48781a17adfc074ccae0e7"), "title" : "Java 教程", "author" : "张三", "phone" : "18812454545" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e8"), "title" : "PHP 教程", "author" : "李四", "phone" : "18912454545" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e9"), "title" : "Python 教程", "author" : "王五", "phone" : "19912454545" }
> db.empDetails.findOneAndUpdate( {author: '李四'}, { $set: {phone:"13888888888"}} );  # 修改
{
"_id" : ObjectId("5f48781a17adfc074ccae0e8"),
"title" : "PHP 教程",
"author" : "李四",
"phone" : "18912454545"
}
> db.empDetails.find(); # 查询修改后的
{ "_id" : ObjectId("5f48781a17adfc074ccae0e7"), "title" : "Java 教程", "author" : "张三", "phone" : "18812454545" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e8"), "title" : "PHP 教程", "author" : "李四", "phone" : "13888888888" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e9"), "title" : "Python 教程", "author" : "王五", "phone" : "19912454545" }

MongoDB updateOne() 方法

此方法更新与给定过滤器匹配的单个文档。
语法:

db.COLLECTION_NAME.updateOne(<filter>, <update>)


db.empDetails.find(); #修改前
{ "_id" : ObjectId("5f48781a17adfc074ccae0e7"), "title" : "Java 教程", "author" : "张三", "phone" : "18812454545" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e8"), "title" : "PHP 教程", "author" : "李四", "phone" : "13888888888" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e9"), "title" : "Python 教程", "author" : "王五", "phone" : "19912454545" }
> db.empDetails.updateOne({author: '王五'},{ $set: { phone: '19999999999'}})  #修改
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.empDetails.find();  # 修改后
{ "_id" : ObjectId("5f48781a17adfc074ccae0e7"), "title" : "Java 教程", "author" : "张三", "phone" : "18812454545" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e8"), "title" : "PHP 教程", "author" : "李四", "phone" : "13888888888" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e9"), "title" : "Python 教程", "author" : "王五", "phone" : "19999999999" }

updateMany()方法

updateMany() 方法更新与给定过滤器匹配的所有文档。

>db.COLLECTION_NAME.update(<filter>, <update>)

示例:

db.empDetails.find();
{ "_id" : ObjectId("5f48781a17adfc074ccae0e7"), "title" : "Java 教程", "author" : "张三", "phone" : "18812454545" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e8"), "title" : "PHP 教程", "author" : "李四", "phone" : "13888888888" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e9"), "title" : "Python 教程", "author" : "王五", "phone" : "19999999999" }
> db.empDetails.updateMany({ $or:[{author:"张三"}, { author:"李四"}] } ,{ $set: { phone: '10086'}});
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.empDetails.find();
{ "_id" : ObjectId("5f48781a17adfc074ccae0e7"), "title" : "Java 教程", "author" : "张三", "phone" : "10086" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e8"), "title" : "PHP 教程", "author" : "李四", "phone" : "10086" }
{ "_id" : ObjectId("5f48781a17adfc074ccae0e9"), "title" : "Python 教程", "author" : "王五", "phone" : "19999999999" }

以上是MongoDB 更新文档的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>