0
0
MongoDBquery~10 mins

Soft delete pattern in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Soft delete pattern in MongoDB
Start: Document exists
Update document: set deleted flag
Document marked as deleted
Queries exclude deleted documents
Document hidden but still in DB
Optional: Restore by unsetting deleted flag
The soft delete pattern updates a document to mark it as deleted instead of removing it, so queries exclude it but data remains.
Execution Sample
MongoDB
db.users.updateOne({_id: 1}, {$set: {deleted: true}})
db.users.find({deleted: {$ne: true}})
Marks a user as deleted and then queries only users not marked deleted.
Execution Table
StepActionQuery/UpdateResult/Effect
1Initial statedb.users.find({})[{_id:1, name:'Alice'}, {_id:2, name:'Bob'}]
2Soft delete user 1db.users.updateOne({_id:1}, {$set: {deleted:true}})User 1 document updated with deleted:true
3Query excluding deleteddb.users.find({deleted: {$ne:true}})[{_id:2, name:'Bob'}]
4Restore user 1db.users.updateOne({_id:1}, {$unset: {deleted: 1}})User 1 document deleted flag removed
5Query excluding deleteddb.users.find({deleted: {$ne:true}})[{_id:1, name:'Alice'}, {_id:2, name:'Bob'}]
6Hard delete user 2db.users.deleteOne({_id:2})User 2 document removed from collection
7Final querydb.users.find({deleted: {$ne:true}})[{_id:1, name:'Alice'}]
💡 Execution ends after final query showing only non-deleted documents.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
users collection[{_id:1, name:'Alice'}, {_id:2, name:'Bob'}][{_id:1, name:'Alice', deleted:true}, {_id:2, name:'Bob'}][{_id:1, name:'Alice'}, {_id:2, name:'Bob'}][{_id:1, name:'Alice'}][{_id:1, name:'Alice'}]
Key Moments - 3 Insights
Why does the soft deleted document still exist in the database?
Because the update sets a 'deleted' flag instead of removing the document, so it stays but is marked as deleted (see Step 2 in execution_table).
How do queries exclude soft deleted documents?
Queries add a condition to exclude documents where 'deleted' is true, like {deleted: {$ne:true}}, shown in Steps 3 and 5.
What is the difference between soft delete and hard delete here?
Soft delete marks the document with a flag (Step 2), hard delete removes it completely (Step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the query at Step 3?
A[{_id:2, name:'Bob'}]
B[{_id:1, name:'Alice'}]
C[{_id:1, name:'Alice'}, {_id:2, name:'Bob'}]
D[]
💡 Hint
Check the 'Result/Effect' column for Step 3 in execution_table.
At which step is the 'deleted' flag removed from user 1?
AStep 2
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the 'Action' and 'Query/Update' columns in execution_table for when $unset is used.
If we never ran Step 6, what would the final query at Step 7 return?
A[{_id:1, name:'Alice'}]
B[{_id:1, name:'Alice'}, {_id:2, name:'Bob'}]
C[]
D[{_id:2, name:'Bob'}]
💡 Hint
Check variable_tracker to see if user 2 is removed or not after Step 6.
Concept Snapshot
Soft delete in MongoDB:
- Update document with a 'deleted' flag instead of removing.
- Queries exclude documents where deleted:true.
- Allows restoring by unsetting the flag.
- Hard delete removes document permanently.
- Keeps data for audit or recovery.
Full Transcript
The soft delete pattern in MongoDB means marking a document as deleted by adding a flag instead of deleting it. This keeps the data but hides it from normal queries by filtering out documents with the deleted flag. You update the document with {$set: {deleted: true}} to soft delete it. Queries then use {deleted: {$ne: true}} to exclude these. You can restore by removing the flag with {$unset: {deleted: ''}}. Hard delete removes the document completely. This pattern helps keep data safe and recoverable while hiding it from users.