首页 / MONGODB / MongoDB 更新文档
MongoDB 更新文档
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MongoDB 更新文档,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3205字,纯文字阅读大概需要5分钟。
内容图文
update() 方法用于更新已存在的文档。语法格式如下:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
参数说明:
query : update的查询条件,类似sql update查询内where后面的。
update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
writeConcern :可选,抛出异常的级别。
实例
我们在集合 mycol 中插入如下数据:
db.mycol.insert({ "_id" : 100,"name":"liang", "title" : "MongoDB Overview" })
db.mycol.insert({ "_id" : 101,"name":"guo", "title" : "MongoDB Guide" })
db.mycol.insert({ "_id" : 102,"name":"jun", "title" : "NoSQL Database" })
db.mycol.insert({ "_id" : 104,"name":"liuzhou", "title" : "Python Quick Guide" })
通过 update() 方法来更新标题(title):
db.mycol.update({"name":"liang"},{$set:{"title":"MongoDB Overview2" }})
db.mycol.find();
{ "_id" : 100, "name" : "liang", "title" : "MongoDB Overview2" }
{ "_id" : 101, "name" : "guo", "title" : "MongoDB Guide" }
{ "_id" : 102, "name" : "jun", "title" : "NoSQL Database" }
{ "_id" : 104, "name" : "liuzhou", "title" : "Python Quick Guide" }
以上语句只会修改第一条发现的文档,如果你要修改多条相同的文档,则需要设置 multi 参数为 true。
db.mycol2.insert({ "_id" : 100,"name":"liang", "title" : "MongoDB Overview" })
db.mycol2.insert({ "_id" : 101,"name":"liang", "title" : "MongoDB Guide" })
db.mycol2.insert({ "_id" : 102,"name":"liang", "title" : "NoSQL Database" })
db.mycol2.insert({ "_id" : 104,"name":"liang", "title" : "Python Quick Guide" })
db.mycol2.update({‘name‘:‘liang‘},{$set:{‘title‘:‘MongoDB‘}},{multi:true})
db.mycol2.find();
{ "_id" : 100, "name" : "liang", "title" : "MongoDB" }
{ "_id" : 101, "name" : "liang", "title" : "MongoDB" }
{ "_id" : 102, "name" : "liang", "title" : "MongoDB" }
{ "_id" : 104, "name" : "liang", "title" : "MongoDB" }
save() 方法
save() 方法通过传入的文档来替换已有文档。语法格式如下:
db.collection.save(
<document>,
{
writeConcern: <document>
}
)
db.mycol3.insert({ "_id" : 100,"name":"liang", "title" : "MongoDB" })
db.mycol3.save({ "_id" : 100,"name":"liang2", "title" : "MongoDB2" })
db.mycol3.find()
{ "_id" : 100, "name" : "liang2", "title" : "MongoDB2" }
更多实例
只更新第一条记录:
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 更新文档
标签:base ase count 更新 size 存在 over 方法 sql
本文系统来源:http://www.cnblogs.com/liang545621/p/7373424.html
内容总结
以上是互联网集市为您收集整理的MongoDB 更新文档全部内容,希望文章能够帮你解决MongoDB 更新文档所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。