0
0
MongodbComparisonBeginner · 4 min read

DeleteOne vs deleteMany in MongoDB: Key Differences and Usage

In MongoDB, 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.

FeaturedeleteOnedeleteMany
PurposeDeletes a single documentDeletes multiple documents
Number of documents deletedAt most oneZero or more (all matching)
Return valueInfo about the deleted documentInfo about all deleted documents
Use caseRemove one specific itemRemove many items matching criteria
PerformanceFaster for single deletesEfficient 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

javascript
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();
Output
Deleted count: 1
↔️

deleteMany Equivalent

javascript
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();
Output
Deleted count: 3
🎯

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.
Use deleteOne for single, precise deletions and deleteMany for bulk removals.
Both methods return the count of deleted documents in the result object.
Be cautious with deleteMany to avoid unintended mass deletions.
Performance-wise, deleteOne is faster for single deletes, deleteMany is efficient for multiple deletes.