删除文档
remove
说明
删除文档.命令格式为db.collection.remove()
.具体参数如下:
db.<collection>.remove( # <collection> 需要操作的集合
<query>, # 匹配的文档筛选条件,当为空删除所有文档
{ # 删除操作参数
justOne: true, # 只删除一篇文档
}
)
示例
默认情况下remove会删除所有满足条件的文档
# 删除name 为hello的文档
> db.qvbilam_user.remove({ name: "hello" })
# 返回内容为被删除的文档数量为3
WriteResult({ "nRemoved" : 3 })
删除一条文档
# 查询num=13的文档数量
> db.qvbilam_user.find({num: 13})
# 返回4条
{ "_id" : ObjectId("5f390671e9b6f99fc04f2b77"), "name" : "GuoTwo", "num" : 13 }
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7a"), "name" : "Coca cola", "num" : 13 }
{ "_id" : ObjectId("5f3d31f16dbe86c585d88d19"), "name" : "MoFan", "num" : 13, "location" : [ "China", "Hebei", "ShiJiaZhuang", "ShenZe" ], "mobile" : 13211111111 }
{ "_id" : ObjectId("5f4767d0421e363aeb8901b0"), "name" : "red", "num" : 13, "skill" : { "work" : "php", "like" : "golang", "hate" : "html" } }
# 只删除一条满足条件的文档
> db.qvbilam_user.remove({num: 13},{justOne: true})
# 返回删除结果
WriteResult({ "nRemoved" : 1 })
# 查询num=13的文档数量
> db.qvbilam_user.find({num: 13})
# 返回3条
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7a"), "name" : "Coca cola", "num" : 13 }
{ "_id" : ObjectId("5f3d31f16dbe86c585d88d19"), "name" : "MoFan", "num" : 13, "location" : [ "China", "Hebei", "ShiJiaZhuang", "ShenZe" ], "mobile" : 13211111111 }
{ "_id" : ObjectId("5f4767d0421e363aeb8901b0"), "name" : "red", "num" : 13, "skill" : { "work" : "php", "like" : "golang", "hate" : "html" } }
>
删除全部文档
# 删除全部文档
> db.qvbilam_user.remove({})
# 删除数量17
WriteResult({ "nRemoved" : 17 })
# 验证
> db.qvbilam_user.find()
# 返回结果为空
>
删除集合
drop
说明
删除整个集合,命令格式为db.collection.drop()
示例
# 删除文档
> db.qvbilam_user.drop()
# 删除成功
true