Bird
Raised Fist0
MongoDBquery~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of the soft delete pattern in MongoDB?
easy
A. To encrypt data before deletion
B. To permanently remove data immediately
C. To backup data before deletion
D. To mark data as deleted without actually removing it from the database

Solution

  1. Step 1: Understand soft delete concept

    Soft delete means marking data as deleted but keeping it in the database.
  2. Step 2: Compare options

    Only To mark data as deleted without actually removing it from the database describes marking data as deleted without removal.
  3. Final Answer:

    To mark data as deleted without actually removing it from the database -> Option D
  4. Quick Check:

    Soft delete = mark, not remove [OK]
Hint: Soft delete means mark deleted, not remove [OK]
Common Mistakes:
  • Confusing soft delete with hard delete
  • Thinking soft delete removes data
  • Assuming soft delete encrypts data
2. Which of the following is the correct way to add a soft delete flag to a MongoDB document?
easy
A. { deleted: true }
B. { isDeleted: 'yes' }
C. { deletedAt: 'no' }
D. { remove: false }

Solution

  1. Step 1: Identify common soft delete fields

    Soft delete usually uses a boolean field like 'deleted' set to true or false.
  2. Step 2: Check options for correct boolean usage

    { deleted: true } uses { deleted: true } which is standard and correct.
  3. Final Answer:

    { deleted: true } -> Option A
  4. Quick Check:

    Soft delete flag = boolean true [OK]
Hint: Use boolean field deleted: true for soft delete [OK]
Common Mistakes:
  • Using string values instead of boolean
  • Using unrelated field names
  • Confusing deletedAt with boolean flag
3. Given the collection documents:
{ _id: 1, name: 'Alice', deleted: false }
{ _id: 2, name: 'Bob', deleted: true }
What will this query return?
db.users.find({ deleted: false })
medium
A. [{ _id: 1, name: 'Alice', deleted: false }]
B. [{ _id: 2, name: 'Bob', deleted: true }]
C. []
D. All documents

Solution

  1. Step 1: Understand the query filter

    The query filters documents where deleted is false.
  2. Step 2: Check documents matching filter

    Only the document with _id 1 has deleted: false, so it is returned.
  3. Final Answer:

    [{ _id: 1, name: 'Alice', deleted: false }] -> Option A
  4. Quick Check:

    Filter deleted: false returns Alice [OK]
Hint: Filter deleted: false to exclude soft deleted [OK]
Common Mistakes:
  • Returning documents with deleted: true
  • Returning empty result incorrectly
  • Assuming query returns all documents
4. You want to update a document to soft delete it by setting deleted: true. Which of these update commands is correct?
medium
A. db.collection.updateOne({ _id: 1 }, { deleted: true })
B. db.collection.updateOne({ _id: 1 }, { $set: { deleted: true } })
C. db.collection.updateOne({ _id: 1 }, { $unset: { deleted: true } })
D. db.collection.updateOne({ _id: 1 }, { $push: { deleted: true } })

Solution

  1. Step 1: Recall MongoDB update syntax

    To update a field, use $set operator with the new value.
  2. Step 2: Analyze options

    db.collection.updateOne({ _id: 1 }, { $set: { deleted: true } }) correctly uses $set to set deleted: true. db.collection.updateOne({ _id: 1 }, { deleted: true }) misses $set, causing replacement. Options C and D use wrong operators.
  3. Final Answer:

    db.collection.updateOne({ _id: 1 }, { $set: { deleted: true } }) -> Option B
  4. Quick Check:

    Use $set to update fields [OK]
Hint: Use $set to update fields in MongoDB [OK]
Common Mistakes:
  • Omitting $set causing document replacement
  • Using $unset instead of $set
  • Using $push on non-array field
5. You want to find all documents including soft deleted ones, but sort them so that non-deleted come first. Which query achieves this?
hard
A. db.collection.find({ deleted: { $exists: false } })
B. db.collection.find({ deleted: false }).sort({ name: 1 })
C. db.collection.find().sort({ deleted: 1 })
D. db.collection.find().sort({ deleted: -1 })

Solution

  1. Step 1: Understand requirement

    We want all documents, including deleted, but sorted so deleted: false first.
  2. Step 2: Analyze sorting by deleted field

    Sorting by deleted: 1 sorts false (0) before true (1), so non-deleted come first.
  3. Step 3: Check options

    db.collection.find().sort({ deleted: 1 }) finds all and sorts by deleted ascending, matching requirement. db.collection.find({ deleted: false }).sort({ name: 1 }) filters out deleted documents. db.collection.find().sort({ deleted: -1 }) sorts deleted descending (deleted first). db.collection.find({ deleted: { $exists: false } }) filters documents missing deleted field.
  4. Final Answer:

    db.collection.find().sort({ deleted: 1 }) -> Option C
  5. Quick Check:

    Sort by deleted ascending puts non-deleted first [OK]
Hint: Sort by deleted: 1 to put non-deleted first [OK]
Common Mistakes:
  • Filtering out deleted documents instead of including all
  • Sorting deleted descending to put deleted first
  • Filtering by missing deleted field