We can delete existing documents by using the deleteOne and deleteMany methods as follows:
- Deleting only one document that matches a condition:
We can delete an existing document like this:
> db.<collection>.deleteOne(<filter>, <options>)
Let's delete the document where the status field equals pending as follows:
> db.user.deleteOne( { status: "pending" } )
You should get the following result:
{ "acknowledged" : true, "deletedCount" : 3 }
- Deleting documents that match a condition:
We can delete multiple existing documents like this:
> db.<collection>.deleteMany(<filter>, <options>)
Let's delete documents where the status field equals ok:
> db.user.deleteMany({ status : "ok" })
You should get the following result:
{ "acknowledged" : true, "deletedCount" : 2 }
- Deleting all documents:
We can delete all documents in a collection by passing an empty...