Welcome 微信登录

首页 / 数据库 / MySQL / MongoDB 文档的删除操作

在db中删除数据是十分危险的事,建议使用logic delete,即在doc中增加一个field:IsDeleted,将其设置为1,表示该doc在逻辑上被删除,这种workaround将delete操作转换为一个update操作,比较安全。MongoDB使用remove删除doc,语法如下,db.collection.remove( <query filter>, { justOne: <boolean>, writeConcern: <document> })query filter={ <field1>: <value1>, ... }{ <field1>: { <operator1>: <value1> }, ... }All write operations in MongoDB are atomic on the level of a single document.1,示例创建users collectionuse testuser1={ name:"t1", age:21}user2={ name:"t2", age:22}user3={ name:"t3", age:23}db.users.insert([user1,user2,user3])2,删除所有doc在query filter中设置empty filter,空的doc,将所有的doc都删除。db.users.remove({})3,删除所有符合query filter的docdb.users.remove({age:21})4,只删除第一个符合query filter的doc,设置justOne 参数为truedb.users.remove({age:{$gt:21}},{justOne:true})5,以原子操作删除所有符合query filter的doc,即在一个原子操作中奖多个doc删除db.users.remove({age:{$gte:21},$isolated:1})参考doc:Delete Documents更多MongoDB相关教程见以下内容:CentOS 编译安装 MongoDB与mongoDB的php扩展 http://www.linuxidc.com/Linux/2012-02/53833.htmCentOS 6 使用 yum 安装MongoDB及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htmUbuntu 13.04下安装MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htmMongoDB入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htmUbunu 14.04下MongoDB的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htmNagios监控MongoDB分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm基于CentOS 6.5操作系统搭建MongoDB服务 http://www.linuxidc.com/Linux/2014-11/108900.htmMongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里本文永久更新链接地址