Three documents are inserted: two with name "Alice" and one with "Bob".
Step 2: Delete documents where name is "Alice"
Both documents with "Alice" are deleted, leaving only the one with "Bob".
Step 3: Count remaining documents
Only one document remains, so the count is 1.
Final Answer:
1 -> Option C
Quick Check:
3 inserted - 2 deleted = 1 left [OK]
Hint: Count after deleteMany equals original minus deleted matches [OK]
Common Mistakes:
Assuming deleteMany deletes only one document
Counting all inserted documents without deletion
Confusing deleteMany with deleteOne
4. What is wrong with this MongoDB delete command?
db.products.deleteOne("category": "electronics")
medium
A. The filter is missing curly braces {}.
B. deleteOne cannot be used with a filter.
C. The collection name is incorrect.
D. The command should be deleteMany instead of deleteOne.
Solution
Step 1: Check the syntax of deleteOne
The filter argument must be an object enclosed in curly braces {}.
Step 2: Identify the error in the command
The filter is written without braces, causing a syntax error.
Final Answer:
The filter is missing curly braces {}. -> Option A
Quick Check:
Filter needs {} in deleteOne [OK]
Hint: Always wrap filter in {} for deleteOne [OK]
Common Mistakes:
Omitting curly braces around filter
Thinking deleteOne cannot take filters
Confusing collection name with command
5. You want to delete all documents where the field status is either "inactive" or missing. Which MongoDB delete command correctly does this while avoiding accidental deletion of other documents?
hard
A. db.users.deleteMany({"status": {$in: ["inactive", null]}})
B. db.users.deleteMany({$or: [{"status": "inactive"}, {"status": {$exists: false}}]})
C. db.users.deleteMany({"status": "inactive", "status": {$exists: false}})
D. db.users.deleteMany({"status": "inactive" || {$exists: false}})
Solution
Step 1: Understand the filter requirements
We want to delete documents where status is "inactive" OR status field does not exist.
Step 2: Analyze each option's filter
db.users.deleteMany({$or: [{"status": "inactive"}, {"status": {$exists: false}}]}) uses $or with correct conditions; B and C have syntax errors; A checks for null but not missing field.
Step 3: Confirm correct syntax and logic
db.users.deleteMany({$or: [{"status": "inactive"}, {"status": {$exists: false}}]}) correctly combines conditions with $or and uses $exists to check missing fields.
Final Answer:
db.users.deleteMany({$or: [{"status": "inactive"}, {"status": {$exists: false}}]}) -> Option B
Quick Check:
Use $or with $exists for missing fields [OK]
Hint: Use $or and $exists to target missing or specific values [OK]
Common Mistakes:
Using || inside filter object (invalid syntax)
Trying to combine conditions with commas incorrectly
Using $in with null instead of $exists for missing fields