Delete in MongoDB
In MongoDB, the operation to delete documents from a collection is performed using the deleteOne() and deleteMany() methods. These methods allow you to delete single or multiple documents from a collection based on the specified criteria.
1. deleteOne()
The deleteOne() method deletes one document that matches the given filter. If there are multiple documents that match the filter, only the first document will be deleted.
Syntax:
db.collection.deleteOne(filter, options)
- filter: The query that defines the conditions to match the document to be deleted.
- options: (Optional) Additional options, such as
writeConcern.
Example:
// Delete a document where the name is "Alice"db.users.deleteOne({ name: "Alice" })
This will delete the first document in the users collection where the name is "Alice". If there are multiple documents with name: "Alice", only the first one encountered will be deleted.
2. deleteMany()
The deleteMany() method deletes all documents that match the given filter. It will remove all documents in the collection that meet the conditions specified in the filter.
Syntax:
db.collection.deleteMany(filter, options)
- filter: The query that defines the conditions to match documents to be deleted.
- options: (Optional) Additional options, such as
writeConcern.
Example:
// Delete all documents where the age is less than 20db.users.deleteMany({ age: { $lt: 20 } })
This will delete all documents in the users collection where the age is less than 20.
3. deleteOne() vs deleteMany()
deleteOne(): Removes a single document that matches the filter. Even if multiple documents match the filter, only the first document will be deleted.
deleteMany(): Removes all documents that match the filter, so all matching documents will be deleted.
4. Deleting All Documents in a Collection
To delete all documents in a collection, you can use the deleteMany() method with an empty filter object {}.
Example:
// Delete all documents in the "users" collectiondb.users.deleteMany({})
This will delete every document in the users collection but will not remove the collection itself. The collection will still exist in the database, but it will be empty.
5. Deleting Documents Using Criteria
You can delete documents based on specific conditions, such as age, name, or other fields.
Example - Delete a document based on age:
// Delete the first document with age greater than 30db.users.deleteOne({ age: { $gt: 30 } })
Example - Delete multiple documents with age less than 18:
// Delete all documents where age is less than 18db.users.deleteMany({ age: { $lt: 18 } })
6. Important Considerations
- Atomic Operation: The
deleteOne()anddeleteMany()methods are atomic operations on a single document or a batch of documents, respectively. - No Rollback: Once documents are deleted, they cannot be easily recovered unless you have backups or use features like the MongoDB Atlas backups.
- Indexing: Deleting documents based on indexed fields (e.g.,
name,_id) can be faster than using unindexed fields.
7. Using delete in MongoDB with MongoDB Atlas (via API)
If you're using the MongoDB Data API in MongoDB Atlas, you can also delete documents by making HTTP requests.
Example:
curl -X POST "https://data.mongodb-api.com/app/<app-id>/endpoint/data/v1/action/deleteOne" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <API-KEY>" \ -d '{ "dataSource": "Cluster0", "database": "myDatabase", "collection": "users", "filter": { "name": "Alice" } }'
This example deletes the first document where the name is "Alice" from the users collection in the myDatabase database.
Summary
deleteOne(): Deletes the first document matching the filter.deleteMany(): Deletes all documents matching the filter.- Both methods use a filter to define which documents to delete.
- Deleting all documents can be done with an empty filter (
{}) usingdeleteMany(). - These operations are atomic and irreversible unless backups are available.