mongoDB文档删除等同于关系型数据库中删除满足条件的单条或者多条记录,通常使用db.collection.remove()方法来实现文档的删除动作。mongDB文档删除属于原子性操作,仅仅在仅仅在单个文档级别。本文描述mongoDB文档删除操作并给出示例。
预备热身
Linux下快速安装MongoDB
Windows平台下安装MongoDB
mongoDB 启动与停止
mongo shell连接到mongoDB及shell提示符下执行js脚本
mongoDB简介及关键特性
SQL与mongoDB对比及映射
一、删除文档的常用方法
1、删除文档常用方法
db.collection.remove()
删除满足匹配条件的一个或多个文档
db.collection.deleteOne()
删除满足匹配条件的最多一个文档(即使有多个文档满足匹配条件),3.2版本支持
db.collection.deleteMany()
删除满足匹配条件的所有文档
单个或集合内的所有文档的删除,与之相应的索引并不会被删除
文档删除的操作属于原子性操作,仅仅在单个文档级别,可以理解为关系型数据库的行级锁
2、语法
db.collection.remove(
<query>, //查询或过滤条件
{
justOne: <boolean>, //用于确定是单行还是删除所有行(true为缺省值即单行)
writeConcern: <document> //设定写安全,用于确保强一致性还是弱一致性
}
)
二、准备环境
[root@node233 ~]# more /etc/redhat-release
CentOS release 6.7 (Final)
# mongo
> db.version()
3.2.10
> db
test
> db.users.insertMany(
[
{
_id: 1,
name: "sue",
age: 19,
type: 1,
status: "P",
favorites: { artist: "Picasso", food: "pizza" },
finished: [ 17, 3 ],
badges: [ "blue", "black" ],
points: [
{ points: 85, bonus: 20 },
{ points: 85, bonus: 10 }
]
},
{
_id: 2,
name: "bob",
age: 42,
type: 1,
status: "A",
favorites: { artist: "Miro", food: "meringue" },
finished: [ 11, 25 ],
badges: [ "green" ],
points: [
{ points: 85, bonus: 20 },
{ points: 64, bonus: 12 }
]
},
{
_id: 3,
name: "ahn",
age: 22,
type: 2,
status: "A",
favorites: { artist: "Cassatt", food: "cake" },
finished: [ 6 ],
badges: [ "blue", "red" ],
points: [
{ points: 81, bonus: 8 },
{ points: 55, bonus: 20 }
]
},
{
_id: 4,
name: "xi",
age: 34,
type: 2,
status: "D",
favorites: { artist: "Chagall", food: "chocolate" },
finished: [ 5, 11 ],
badges: [ "red", "black" ],
points: [
{ points: 53, bonus: 15 },
{ points: 51, bonus: 15 }
]
},
{
_id: 5,
name: "xyz",
age: 23,
type: 2,
status: "D",
favorites: { artist: "Noguchi", food: "nougat" },
finished: [ 14, 6 ],
badges: [ "orange" ],
points: [
{ points: 71, bonus: 20 }
]
},
{
_id: 6,
name: "abc",
age: 43,
type: 1,
status: "A",
favorites: { food: "pizza", artist: "Picasso" },
finished: [ 18, 12 ],
badges: [ "black", "blue" ],
points: [
{ points: 78, bonus: 8 },
{ points: 57, bonus: 7 }
]
}
]
)
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4, 5, 6 ] }
三、演示文档删除
1、db.collection.deleteOne
//语法如下
db.collection.deleteOne(
<filter>, //查询的过滤条件
{
writeConcern: <document> //用于控制写入何时应答及超时
}
)
> db.users.find({status:"A"}).count()
3
> db.users.deleteOne({status:"A"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.users.find({status:"A"}).count()
2
2、删除多个文档
//语法,参数同deleteOne
db.collection.deleteMany(
<filter>,
{
writeConcern: <document>
}
)
> db.users.deleteMany({status:"A"})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.users.find({status:"A"}).count()
0
3、db.collection.remove
//语法
db.collection.remove(
<query>, //过滤条件
{
justOne: <boolean>, //可选条件,布尔类,为true时,仅删除满足条件单个文档,否则删除满足条件全部文档
writeConcern: <document> ////写安全参数,用于控制写入何时应答及超时
}
)
//对于db.collection.remove,当justOne为true时等同于deleteOne方法,为false时等同于deleteMany方法
//如下,当不指定任何参数的时候,收到错误提示,即需要指定过滤条件,防止误清空整个集合
> db.users.remove()
2016-10-21T23:48:31.766+0800 E QUERY [thread1] Error: remove needs a query :
DBCollection.prototype._parseRemove@src/mongo/shell/collection.js:406:1
DBCollection.prototype.remove@src/mongo/shell/collection.js:433:18
@(shell):1:1
> db.users.find({status:"D"}).count()
2
> db.users.createIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1 //Author : Leshami
}, //Blog : http://blog.csdn.net/leshami
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test.users"
}
]
//删除满足状态为D的任意一个文档
> db.users.remove({status:"D"},true)
WriteResult({ "nRemoved" : 1 })
//删除满足年龄大于20的所有文档
> db.users.remove({age:{$gt:20}})
WriteResult({ "nRemoved" : 1 })
//删除满足年龄小于20的所有文档
//且writeConcern参数w为majority,该参数通常用于副本集中,确定有几个节点写入成功才应答给客户端
//此列中表明,满足大多数即可返回应答给客户端,如3个节点复制集,2个节点写入即可返回应答
//wtimeout为写入超时参数,避免写入因意外故障导致挂起,陷入无限等待
> db.users.remove(
{ age: { $lt: 20 } },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
)
WriteResult({ "nRemoved" : 1 })
//此时查看集合,已经无文档返回
> db.users.find()
//查看索引,索引依旧存在,尽管没有任何文档
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test.users"
}
]
> db.users.dropIndexes()
{
"nIndexesWas" : 2,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
//下面直接删除这个空集合
> db.users.drop()
true
//再次查看当前数据库下已经无任何集合对象
> show collections;
四、小结
1、文档的移除通过使用db.collection.remove()方法来实现。
2、db.collection.remove()方法,当justOne为true时等同于db.collection.deleteOne
3、db.collection.remove()方法,当justOne为false时等同于db.collection.deleteMany
4、文档的删除以及全部删除后,对于已经创建的索引依旧存在,可以单独删除(dropIndexes)或者删除整个集合
5、参数writeConcern: <document>在副本集的时候用于副本集数据库一致性