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
Soft Delete Pattern in MongoDB
📖 Scenario: You are managing a user database for a web application. Instead of permanently deleting user records, you want to mark them as deleted so you can restore them later if needed.
🎯 Goal: Build a MongoDB collection with a soft delete pattern by adding a deleted field to user documents and write queries to filter out deleted users.
📋 What You'll Learn
Create a users collection with sample user documents
Add a deleted field to each user document to mark soft deletion
Write a query to find only users who are not deleted
Write a query to mark a user as deleted by setting deleted to true
💡 Why This Matters
🌍 Real World
Soft delete is used in many applications to avoid losing data permanently and to allow recovery of deleted records.
💼 Career
Understanding soft delete patterns is important for database management roles and backend development jobs where data integrity and recovery are critical.
Progress0 / 4 steps
1
Create the users collection with sample data
Create a users collection with these exact documents: { _id: 1, name: "Alice", deleted: false }, { _id: 2, name: "Bob", deleted: false }, and { _id: 3, name: "Charlie", deleted: false }.
MongoDB
Hint
Use db.users.insertMany() to add multiple documents at once.
2
Add a helper variable for the soft delete filter
Create a variable called activeUsersFilter and set it to { deleted: false } to select only users who are not deleted.
MongoDB
Hint
Use const activeUsersFilter = { deleted: false } to create the filter.
3
Write a query to find all active (not deleted) users
Write a query using db.users.find() with the filter variable activeUsersFilter to find all users where deleted is false.
MongoDB
Hint
Use db.users.find(activeUsersFilter) to get active users.
4
Mark a user as deleted using an update query
Write an update query using db.users.updateOne() to set deleted to true for the user with _id equal to 2.
MongoDB
Hint
Use db.users.updateOne({ _id: 2 }, { $set: { deleted: true } }) to mark the user as deleted.
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
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 D
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
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 A
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
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.