Concept Flow - deleteOne method
Start deleteOne call
Find first matching document
Delete that document
Return delete result
End
The deleteOne method finds the first document matching the filter and deletes it, then returns the result.
db.collection.deleteOne({ age: 25 })| Step | Action | Filter Applied | Document Found | Delete Action | Result |
|---|---|---|---|---|---|
| 1 | Call deleteOne | { age: 25 } | Search starts | No delete yet | No result yet |
| 2 | Find first match | { age: 25 } | { _id: 101, name: 'Alice', age: 25 } | Ready to delete | No result yet |
| 3 | Delete document | { age: 25 } | { _id: 101, name: 'Alice', age: 25 } | Document deleted | Delete acknowledged |
| 4 | Return result | { age: 25 } | N/A | N/A | { acknowledged: true, deletedCount: 1 } |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| filter | undefined | { age: 25 } | { age: 25 } | { age: 25 } |
| foundDocument | null | { _id: 101, name: 'Alice', age: 25 } | null | null |
| deleteResult | null | null | { acknowledged: true, deletedCount: 1 } | { acknowledged: true, deletedCount: 1 } |
deleteOne(filter)
- Finds the first document matching filter
- Deletes that document only
- Returns { acknowledged: true, deletedCount: 1 } if deleted
- If no match, deletedCount is 0
- Stops after one deletion