MongoDB更新文档

您所在的位置:网站首页 mongodb替换文档 MongoDB更新文档

MongoDB更新文档

2024-07-05 06:59| 来源: 网络整理| 查看: 265

在 MongoDB 中,可以使用 update() 和 save() 方法来更新集合中的文档。其中 update() 方法可以更新现有文档中的值,而 save() 方法则可以使用传入文档来替换已有文档。 update() 方法 update() 方法用于更新现有文档中的值,其语法格式如下:

db.collection_name.update(     ,     ,     {         upsert: ,         multi: ,         writeConcern:     } )

参数说明如下: query:update 的查询条件,类似 SQL 中 update 语句内 where 后面的内容; update:update 的对象和一些更新的操作符(如 $、$inc...)等,也可以理解为 SQL 中 update 语句内 set 后面的内容; upsert:可选参数,默认值为 false,用来定义当要更新的记录不存在时,是否当作新记录插入到集合中,当值为 true 时表示插入,值为 false 时不插入; multi:可选参数,默认值为 false,用来表示只更新找到的第一条记录,当值为 true 时,则把按条件查出来的多条记录全部更新; writeConcern:可选参数,用来定义抛出异常的级别。 【示例】首先我们先向集合中插入以下数据: > db.course.insert([ ... { ... title: 'MongoDB教程', ... author: '编程帮', ... url: 'http://www.biancheng.com/' ... },{ ... title: 'HTML教程', ... author: '编程帮', ... url: 'http://www.biancheng.com/html/index.html' ... },{ ... title: 'C#教程', ... author: '编程帮', ... url: 'http://www.biancheng.com/csharp/index.html' ... } ... ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) 接着使用 update() 方法修改 title 为“MongoDB教程”的文档中的内容。 > db.course.update({title:"MongoDB教程"},{$set:{url:"http://www.biancheng.net/mongodb/index.html"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.course.find().pretty() { "_id" : ObjectId("603209d8e492ab9be9450304"), "title" : "MongoDB教程", "author" : "编程帮", "url" : "http://www.biancheng.net/mongodb/index.html" } { "_id" : ObjectId("603209d8e492ab9be9450305"), "title" : "HTML教程", "author" : "编程帮", "url" : "http://www.biancheng.com/html/index.html" } { "_id" : ObjectId("603209d8e492ab9be9450306"), "title" : "C#教程", "author" : "编程帮", "url" : "http://www.biancheng.com/csharp/index.html" } 默认情况下,在使用 update() 方法更新文档时仅会更新一个文档,若想要更新多个文档,您则需要将参数“multi”设置为 true,如下所示:

db.course.update({title:"MongoDB教程"},{$set:{url:"http://www.biancheng.net/mongodb/index.html"}},{multi:true})

save() 方法 MongoDB 中的 save() 方法可以使用传入的文档来替换已有文档,若 _id 主键存在则更新已有文档,若 _id 主键不存在则作为新数据插入到集合中。语法格式如下:

db.collection_name.save(     ,     {         writeConcern:     } )

参数说明如下: document : 文档数据; writeConcern :可选,抛出异常的级别。 【示例】使用 save() 方法更新 _id 为“603209d8e492ab9be9450304”的文档中的数据。 > db.course.save( ... { ... "_id" : ObjectId("603209d8e492ab9be9450304"), ... "title" : "MongoDB教程", ... "description" : "MongoDB 是一个 Nosql 数据库", ... "author" : "编程帮", ... "url" : "http://www.biancheng.net/mongodb/index.html", ... "likes" : 996 ... } ... ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 更新成功后您可以使用 find() 方法来查看更新后的数据。 > db.course.find().pretty() { "_id" : ObjectId("603209d8e492ab9be9450304"), "title" : "MongoDB教程", "description" : "MongoDB 是一个 Nosql 数据库", "author" : "编程帮", "url" : "http://www.biancheng.net/mongodb/index.html", "likes" : 996 } { "_id" : ObjectId("603209d8e492ab9be9450305"), "title" : "HTML教程", "author" : "编程帮", "url" : "http://www.biancheng.com/html/index.html" } { "_id" : ObjectId("603209d8e492ab9be9450306"), "title" : "C#教程", "author" : "编程帮", "url" : "http://www.biancheng.com/csharp/index.html" }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3