MongoDB中怎样进行删除文档,方法是什么
Admin 2022-11-23 群英技术资讯 1470 次浏览
MongoDB是一个跨平台,面向文档的数据库,提供高性能,高可用性和易于扩展。MongoDB是工作在集合和文档上一种概念。
文档是一组键值对。文档具有动态模式。动态模式是指,在同一个集合的文件不必具有相同一组集合的文档字段或结构,并且相同的字段可以保持不同类型的数据。
db.集合名称.remove({query}, justOne)
query:过滤条件,可选
justOne:是否只删除查询到的第一条数据,值为true或者1时,只删除一条数据,默认为false,可选。
准备数据:把_id为1和2的age都变成28
> db.student.update({_id:1},{$set:{age:28}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.update({_id:2},{$set:{age:28}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 28 }
{ "_id" : 2, "name" : "lisi", "age" : 28 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
{ "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "java" ] }
1、使用两个参数:
删除age=28的第一条数据
> db.student.remove({age:28}, true) WriteResult({ "nRemoved" : 1 }) > db.student.find() { "_id" : 2, "name" : "lisi", "age" : 28 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "java" ] }
2、使用一个参数:
删除age=28的所有数据
> db.student.remove({age:28}) WriteResult({ "nRemoved" : 2 }) > db.student.find() { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "java" ] }
3、删除集合的全部数据,括号中的“{}”必须有,表示空的过滤条件:
> db.student.remove({}) WriteResult({ "nRemoved" : 4 })
另外使用remove()方法删除的时候,只是删除数据,表还会存在。使用drop()方法会把表也删除,并且drop()的效率要比remove()效率高很多。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
使用用户名和密码连接到MongoDB数据库服务器,必须使用 'username:password@hostname/dbname' 格式,'username'为用户名,'password' 为密码。
Linux平台安装MongoDB MongoDB 提供了 linux 各个发行版本 64 位的安装包,你可以在官网下载安装包。 安装前我们需要安装各个 Linux 平台依赖包。 Red Hat/CentOS: sudo yum install libcurl openssl Ubuntu 18.04 LTS (“Bionic”)/Debian 10 “Buster”: sudo apt-get install libcurl4 o..
MongoDB 高级索引 考虑以下文档集合(users ): { 'address': { 'city': 'Los Angeles', 'state': 'California', 'pincode': '123' }, 'tags..
文本给大家介绍是关于MongoDB关系,很多新手刚接触MongoDB数据库,可能对于MongoDB 的关系不是很清楚,因此下面给大家简单介绍一下MongoDB关系的定义,嵌入式关系和引用式关系。
这篇文章给大家介绍了mongodb索引的建立,删除索引的方法以及唯一索引和组合索引的知识,感兴趣的朋友一起看看吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008