小白教程
所有教程
关于
Search
172.69.58.224
172.69.58.224
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“MongoDB 更新文档”的源代码
本页内容
上一节:
MongoDB_插入文档
下一节:
MongoDB_删除文档
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:MongoDB 更新文档}}[[Category:MongoDB 教程|14]] = MongoDB 更新文档 = MongoDB 使用 '''update()''' 和 '''save()''' 方法来更新集合中的文档。接下来让我们详细来看下两个函数的应用及其区别。 == update() 方法 == update() 方法用于更新已存在的文档。语法格式如下: <sample title="" desc="" lang="mongodb" hererun="1"> db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } ) </sample> '''参数说明:''' * '''query''' : update的查询条件,类似sql update查询内where后面的。 * '''update''' : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的 * '''upsert''' : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。 * '''multi''' : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。 * '''writeConcern''' :可选,抛出异常的级别。 == 我们在集合 col 中插入如下数据: <sample title="" desc="" lang="mongodb" hererun="1"> >db.col.insert({ title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: '小白教程', url: 'http://www.xiaobai.wang', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) </sample> 接着我们通过 update() 方法来更新标题(title): <sample title="" desc="" lang="mongodb" hererun="1"> >db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) # 输出信息 > db.col.find().pretty() { "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "小白教程", "url" : "http://www.xiaobai.wang", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } > </sample> 可以看到标题(title)由原来的 "MongoDB 教程" 更新为了 "MongoDB"。 以上语句只会修改第一条发现的文档,如果你要修改多条相同的文档,则需要设置 multi 参数为 true。 <sample title="" desc="" lang="mongodb" hererun="1"> >db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true}) </sample> == save() 方法 == save() 方法通过传入的文档来替换已有文档,_id 主键存在就更新,不存在就插入。语法格式如下: <sample title="" desc="" lang="mongodb" hererun="1"> db.collection.save( <document>, { writeConcern: <document> } ) </sample> '''参数说明:''' * '''document''' : 文档数据。 * '''writeConcern''' :可选,抛出异常的级别。 == 以下实例中我们替换了 _id 为 56064f89ade2f21f36b03136 的文档数据: <sample title="" desc="" lang="mongodb" hererun="1"> >db.col.save({ "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "xiaoBai", "url" : "http://www.xiaobai.wang", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 }) </sample> 替换成功后,我们可以通过 find() 命令来查看替换后的数据 <sample title="" desc="" lang="mongodb" hererun="1"> >db.col.find().pretty() { "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "xiaoBai", "url" : "http://www.xiaobai.wang", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 } > </sample> == 更多实例 == 只更新第一条记录: db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 全部更新: db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 只添加第一条: db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 全部添加进去: db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全部更新: db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true ); 只更新第一条记录: db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );
返回至“
MongoDB 更新文档
”。
上一节:
MongoDB_插入文档
下一节:
MongoDB_删除文档