Deleting Documents in MongoDB
Deleting Documents in MongoDB
Deleting documents in MongoDB involves removing one or more documents from a collection. MongoDB provides several methods for deletion, allowing you to target specific documents or clear entire collections.
Methods for Deleting Documents
deleteOne()
: Deletes a single document that matches a query.deleteMany()
: Deletes multiple documents that match a query.findOneAndDelete()
: Finds a single document, deletes it, and optionally returns the deleted document.drop()
: Drops (deletes) an entire collection.dropDatabase()
: Drops (deletes) an entire database.
1. deleteOne()
The deleteOne()
method deletes a single document that matches the query criteria. If multiple documents match the query, only the first one found will be deleted.
Syntax:
db.collection.deleteOne(filter)
- filter: The query to select the document(s) to delete.
Example: Delete a Single Document
To delete a user with a specific email:
db.users.deleteOne({ email: "johndoe@example.com" })
In this example, only the first document with the email "johndoe@example.com"
will be deleted.
2. deleteMany()
The deleteMany()
method deletes all documents that match the query criteria.
Syntax:
db.collection.deleteMany(filter)
- filter: The query to select the documents to delete.
Example: Delete Multiple Documents
To delete all users with an age less than 30:
db.users.deleteMany({ age: { $lt: 30 } })
In this example, all documents in the users
collection with an age less than 30
will be deleted.
3. findOneAndDelete()
The findOneAndDelete()
method finds a single document that matches the query, deletes it, and optionally returns the deleted document.
Syntax:
db.collection.findOneAndDelete(filter, options)
- filter: The query to select the document to delete.
- options: Optional settings, including
returnOriginal
(defaulttrue
, returns the document before deletion).
Example: Find and Delete a Document
To find and delete a user with a specific email and return the deleted document:
db.users.findOneAndDelete({ email: "johndoe@example.com" }, { returnOriginal: false })
In this example, the findOneAndDelete()
method deletes the document with the email "johndoe@example.com"
and returns the deleted document.
4. drop()
The drop()
method deletes an entire collection. This operation removes all documents within the collection and also removes the collection itself from the database.
Syntax:
db.collection.drop()
Example: Drop a Collection
To delete the entire users
collection:
db.users.drop()
In this example, the users
collection will be removed from the database, including all its documents.
5. dropDatabase()
The dropDatabase()
method deletes an entire database. This operation removes all collections and documents within the database.
Syntax:
db.dropDatabase()
Example: Drop a Database
To delete the current database:
db.dropDatabase()
In this example, the database you are currently using will be removed, including all its collections and documents.
Deletion Considerations
- Cascading Deletes: If you have related data across multiple collections and want to maintain referential integrity, you need to handle cascading deletes manually.
- Safety and Permissions: Be cautious with delete operations, as they are irreversible. Ensure you have proper permissions and back up your data if needed.
- Indexes: Dropping a collection will also remove its indexes. If you recreate the collection, you will need to re-create any necessary indexes.
Example: Complete Deletion Operations
Delete a single user by email:
db.users.deleteOne({ email: "johndoe@example.com" })
Delete all products with price greater than 100:
db.products.deleteMany({ price: { $gt: 100 } })
Find and delete a specific order and return it:
db.orders.findOneAndDelete({ order_id: 12345 }, { returnOriginal: false })
Drop the
orders
collection:db.orders.drop()
Drop the current database:
db.dropDatabase()