In this chapter, we will learn how to delete a record the use of MongoDB.
The put off() Method
MongoDB's do away with() technique is used to get rid of a report from the gathering. Dispose of() technique accepts parameters. One is deletion criteria and 2nd is justOne flag.
- Deletion criteria − (Optional) deletion standards in step with files could be removed.
- JustOne − (Optional) if set to true or 1, then put off best one file.
Syntax
Basic syntax of dispose of() approach is as follows −
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
Consider the mycol series has the subsequent records.
{_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"},
{_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"},
{_id : ObjectId("507f191e810c19729de860e3"), title: "Tutorials Point Overview"}
Following instance will get rid of all the files whose identify is 'MongoDB Overview'.
>db.mycol.remove({'title':'MongoDB Overview'})
WriteResult({"nRemoved" : 1})
> db.mycol.find()
{"_id" : ObjectId("507f191e810c19729de860e2"), "title" : "NoSQL Overview" }
{"_id" : ObjectId("507f191e810c19729de860e3"), "title" : "Tutorials Point Overview" }
Remove Only One
If there are more than one statistics and you need to delete most effective the primary record, then set justOne parameter in take away() approach.
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
Remove All Documents
If you don't specify deletion standards, then MongoDB will delete whole files from the collection. This is equal of SQL's truncate command.
> db.mycol.remove({})
WriteResult({ "nRemoved" : 2 })
> db.mycol.find()
>