DeleteOne vs deleteMany in MongoDB: Key Differences and Usage
deleteOne removes a single document matching the filter, while deleteMany deletes all documents matching the filter. Use deleteOne when you want to remove just one document, and deleteMany when you want to remove multiple documents at once.Quick Comparison
Here is a quick side-by-side comparison of deleteOne and deleteMany in MongoDB.
| Feature | deleteOne | deleteMany |
|---|---|---|
| Purpose | Deletes a single document | Deletes multiple documents |
| Number of documents deleted | At most one | Zero or more (all matching) |
| Return value | Info about the deleted document | Info about all deleted documents |
| Use case | Remove one specific item | Remove many items matching criteria |
| Performance | Faster for single deletes | Efficient for bulk deletes |
Key Differences
deleteOne deletes only the first document that matches the filter criteria. If multiple documents match, only one is removed, and the rest remain untouched. This is useful when you want to ensure only a single document is deleted, such as removing a specific user or record.
On the other hand, deleteMany deletes all documents that match the filter. This means if your filter matches 10 documents, all 10 will be removed. This is helpful when you want to clean up or remove multiple records at once, like deleting all expired sessions.
Both methods return a result object that includes the count of deleted documents, but deleteOne will report at most one deletion, while deleteMany can report many. Choosing between them depends on whether you want to delete a single document or multiple documents matching your criteria.
Code Comparison
const { MongoClient } = require('mongodb'); async function deleteOneExample() { const client = new MongoClient('mongodb://localhost:27017'); try { await client.connect(); const db = client.db('testdb'); const collection = db.collection('users'); // Delete one user with name 'Alice' const result = await collection.deleteOne({ name: 'Alice' }); console.log('Deleted count:', result.deletedCount); } finally { await client.close(); } } deleteOneExample();
deleteMany Equivalent
const { MongoClient } = require('mongodb'); async function deleteManyExample() { const client = new MongoClient('mongodb://localhost:27017'); try { await client.connect(); const db = client.db('testdb'); const collection = db.collection('users'); // Delete all users with age less than 18 const result = await collection.deleteMany({ age: { $lt: 18 } }); console.log('Deleted count:', result.deletedCount); } finally { await client.close(); } } deleteManyExample();
When to Use Which
Choose deleteOne when you want to remove a single, specific document and ensure no more than one is deleted. This is common when deleting by a unique identifier or when only one record should be affected.
Choose deleteMany when you need to remove multiple documents that match a condition, such as clearing out all expired records or bulk deleting items. It is more efficient for batch deletions.
Always be careful with deleteMany to avoid accidentally deleting more documents than intended.
Key Takeaways
deleteOne deletes only one matching document, deleteMany deletes all matching documents.deleteOne for single, precise deletions and deleteMany for bulk removals.deleteMany to avoid unintended mass deletions.deleteOne is faster for single deletes, deleteMany is efficient for multiple deletes.