What if deleting data forever was a mistake you could easily fix?
Why Soft delete pattern in MongoDB? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a list of customer records in your database. When someone wants to remove a customer, you delete their record completely. Later, you realize you need to recover some deleted customers or track who was deleted and when.
Deleting records permanently means you lose all history. If you want to undo a deletion or audit past data, you have no way to do it. Manually keeping backups or logs is slow, confusing, and easy to mess up.
The soft delete pattern solves this by marking records as deleted instead of removing them. You add a simple flag like isDeleted: true. This way, data stays safe and recoverable, while your app ignores deleted items by default.
db.customers.deleteOne({ _id: 123 })db.customers.updateOne({ _id: 123 }, { $set: { isDeleted: true } })Soft delete lets you safely hide data without losing it, enabling easy recovery, auditing, and better data management.
A company wants to keep track of all users who left their service but still be able to restore their accounts if they return. Using soft delete, they mark users as deleted instead of erasing them.
Permanent deletion loses valuable data and history.
Soft delete marks data as deleted without removing it.
This pattern improves recovery, auditing, and data safety.
Practice
Solution
Step 1: Understand soft delete concept
Soft delete means marking data as deleted but keeping it in the database.Step 2: Compare options
Only To mark data as deleted without actually removing it from the database describes marking data as deleted without removal.Final Answer:
To mark data as deleted without actually removing it from the database -> Option DQuick Check:
Soft delete = mark, not remove [OK]
- Confusing soft delete with hard delete
- Thinking soft delete removes data
- Assuming soft delete encrypts data
Solution
Step 1: Identify common soft delete fields
Soft delete usually uses a boolean field like 'deleted' set to true or false.Step 2: Check options for correct boolean usage
{ deleted: true } uses { deleted: true } which is standard and correct.Final Answer:
{ deleted: true } -> Option AQuick Check:
Soft delete flag = boolean true [OK]
- Using string values instead of boolean
- Using unrelated field names
- Confusing deletedAt with boolean flag
{ _id: 1, name: 'Alice', deleted: false }{ _id: 2, name: 'Bob', deleted: true }What will this query return?
db.users.find({ deleted: false })Solution
Step 1: Understand the query filter
The query filters documents where deleted is false.Step 2: Check documents matching filter
Only the document with _id 1 has deleted: false, so it is returned.Final Answer:
[{ _id: 1, name: 'Alice', deleted: false }] -> Option AQuick Check:
Filter deleted: false returns Alice [OK]
- Returning documents with deleted: true
- Returning empty result incorrectly
- Assuming query returns all documents
deleted: true. Which of these update commands is correct?Solution
Step 1: Recall MongoDB update syntax
To update a field, use $set operator with the new value.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.Final Answer:
db.collection.updateOne({ _id: 1 }, { $set: { deleted: true } }) -> Option BQuick Check:
Use $set to update fields [OK]
- Omitting $set causing document replacement
- Using $unset instead of $set
- Using $push on non-array field
Solution
Step 1: Understand requirement
We want all documents, including deleted, but sorted so deleted: false first.Step 2: Analyze sorting by deleted field
Sorting by deleted: 1 sorts false (0) before true (1), so non-deleted come first.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.Final Answer:
db.collection.find().sort({ deleted: 1 }) -> Option CQuick Check:
Sort by deleted ascending puts non-deleted first [OK]
- Filtering out deleted documents instead of including all
- Sorting deleted descending to put deleted first
- Filtering by missing deleted field
