Skip to content

Deleting Documents

Estimated time to read: 1 minute

Documents and Collections can be deleted from MongoDB as needed.

Data Explorer

Within the Atlas Data Explorer, selecting either a collection or a document or a database, then selecting the trash can icon within the UI will allow deletion of the specified collection or document.

Mongo Shell

Similar to the find and update commands, the delete command can also be used within Mongo Shell to delete collections, documents or databases.

deleteOne() - To delete an individual collection, document or database. When using this command, best practice is to delete the specified object by referencing the _id field's value. For example, deleteOne("_id":11)

deleteMany() - To delete many collections, documents or databases that match the entered MQL query.

Document Deletion example

mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin" - Connect to the MongoDB Cluster

use sample_training - Specify the collection to be used

db.inspections.find({ "test": 1 }).pretty() - Show all documents that have a test field with a value of 1

db.inspections.find({ "test": 3 }).pretty() - Show all documents that have a test field with a value of 3

db.inspections.deleteMany({ "test": 1 }) - Delete all documents that have a test field with a value of 1

db.inspections.deleteOne({ "test": 3 }) - Delete the first returned document that has a test field with a value of 3

db.inspection.drop() - Drops the entire collection

Note

Deleting all of collections within a database also removes the database itself!