remove()方法
MongoDB的remove() 方法用于从集合中删除文档。remove()方法接受两个参数。第一个是删除condition,第二个是justOne标志。
- condition
-(可选)根据文档的删除标准将被删除。
- justOne
-(可选)如果设置为true或1,则仅删除一个文档。
语法
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
示例
考虑到mycol集合具有以下数据。
{ "_id" : ObjectId("5f4871eb69ad8645590791a2"), "title" : "Tutorials Point New Topic Good good", "by" : "蝴蝶教程" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a3"), "title" : "NoSQL Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a4"), "title" : "Tutorials Point Overview" }
以下示例将为标题为“NoSQL Overview”所有文档删除
> db.mycol.find(); #删除前
{ "_id" : ObjectId("5f4871eb69ad8645590791a2"), "title" : "Tutorials Point New Topic Good good", "by" : "蝴蝶教程" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a3"), "title" : "NoSQL Overview" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a4"), "title" : "Tutorials Point Overview" }
> db.mycol.remove({'title':'NoSQL Overview'}); # 删除
WriteResult({ "nRemoved" : 1 })
> db.mycol.find(); #删除后
{ "_id" : ObjectId("5f4871eb69ad8645590791a2"), "title" : "Tutorials Point New Topic Good good", "by" : "蝴蝶教程" }
{ "_id" : ObjectId("5f4871eb69ad8645590791a4"), "title" : "Tutorials Point Overview" }