Given a MongoDB collection users where soft deleted documents have deletedAt field set to a timestamp, which query returns only active (not soft deleted) users?
db.users.find({ deletedAt: { $exists: false } })Think about how to check if a field is missing in MongoDB documents.
Soft deleted documents have the deletedAt field set. Active documents do not have this field. Using $exists: false filters documents where deletedAt is missing.
How to count the number of soft deleted documents in a collection products where soft delete is indicated by a deletedAt field with a timestamp?
db.products.countDocuments({ deletedAt: { $exists: true } })Soft deleted documents have the deletedAt field present.
Counting documents where deletedAt exists counts all soft deleted documents.
Which update query correctly marks a document as soft deleted by setting deletedAt to the current date?
db.orders.updateOne({ _id: 123 }, { $set: { deletedAt: new Date() } })Remember the correct syntax for MongoDB update operators.
The $set operator is used to assign a new value to a field. Option A uses the correct syntax.
What is the main advantage of using soft delete (marking documents as deleted) over hard delete (removing documents) in MongoDB?
Think about data safety and recovery.
Soft delete keeps the data in the database, allowing recovery or auditing later. Hard delete removes data permanently.
You have a large customers collection with many soft deleted documents marked by deletedAt. Which index improves performance for queries filtering active customers (deletedAt missing)?
Think about indexing the field used in the filter.
Indexing deletedAt helps MongoDB quickly find documents where deletedAt does not exist or is null, improving query speed.