db.collection.remove(
<query>,
{
justOne: <boolean>, writeConcern: <document>
}
)
>db.test.insert(
{
title : 'MongoDB',
description : 'MongoDB 是一个 NoSQL 数据库',
by : 'C语言中文网',
tags : ['mongodb', 'database', 'NoSQL'],
likes : 100
}
)
使用 find() 函数查询的代码如下:
> db.test.find()
{ "_id" : Objectld ("5ba9d8b:L24857a5fefclfde6"), "titlen : "MongoDB", "description" : "MongoDB 是一个 NoSQL 数据库", "by" : "C语言中文网", "tags" : [ "mongodb", "database", "NoSQL" ], "Tikes" : 100 }
{ "_id" : ObjectId("5ba9d90924857a5fefclfde7"), "title" : "MongoDB ", "description" : "MongoDB 是一个 NoSQL 数据库", "by" : "C语言中文网", "tags" : [ "mongodb", "database", "NoSQL"], "likes" : 100 }
接下来移除 title 为“MongoDB”的文档,执行以下操作后,查询会发现两个文档记录均被删除:
>db.test.remove({'title': 'MongoDB'})
WriteResult({ 'nRemoved' : 2 }) #删除了两条数据
另外,可以设置比较条件,如下操作为删除 price 大于 3 的文档记录:
>db.test.remove(
{
price:{$gt:3}
}
)
db.collection.deleteMany ({})
db.collection.deleteMany ({ status : "A" })
db.collection.delete.One ({ status : "D" })
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有