This content originally appeared on DEV Community and was authored by KAWSAR KABIR
To delete documents in MongoDB, you can use the deleteOne()
or deleteMany()
methods. These methods allow you to remove one or multiple documents, respectively.
Here’s an example of using the deleteOne()
method to remove a single document:
db.collection.deleteOne({ _id: ObjectId("your_document_id_here") });
If you want to delete all documents, you can use the deleteMany()
method:
db.collection.deleteMany({});
Additionally, you can delete all documents that match a specific condition:
db.collection.deleteMany({ your_query_here });
To delete an entire collection, use the drop()
method:
db.collection.drop();
This method removes the collection entirely, so it should be used with caution, as all data will be permanently deleted. Make sure to back up your data before performing this operation.
This content originally appeared on DEV Community and was authored by KAWSAR KABIR
KAWSAR KABIR | Sciencx (2024-06-22T04:30:28+00:00) Delete Documents, Drop Collection in MongoDB. Retrieved from https://www.scien.cx/2024/06/22/delete-documents-drop-collection-in-mongodb/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.