The deleteOne method removes a single document from a MongoDB collection that matches a given condition. It helps keep your data clean by deleting unwanted or outdated information.
deleteOne method in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
db.collection.deleteOne(filter, options)
filter is an object that specifies which document to delete.
options is optional and can control things like write concern.
Examples
MongoDB
db.users.deleteOne({ name: "Alice" })MongoDB
db.products.deleteOne({ price: { $lt: 10 } })MongoDB
db.orders.deleteOne({ status: "cancelled" }, { writeConcern: { w: "majority" } })Sample Program
This example first adds three items to the items collection. Then it deletes one document where the name is "Pen". Finally, it shows the remaining documents.
MongoDB
use shopDB // Insert sample documents db.items.insertMany([ { name: "Pen", price: 1.5 }, { name: "Notebook", price: 3 }, { name: "Pen", price: 2 } ]) // Delete one document where name is "Pen" db.items.deleteOne({ name: "Pen" }) // Find all remaining documents db.items.find().toArray()
Important Notes
deleteOne deletes only the first matching document it finds.
If no document matches the filter, nothing is deleted and no error occurs.
Always double-check your filter to avoid deleting the wrong document.
Summary
deleteOne removes a single document matching your filter.
It is useful for deleting specific entries without affecting others.
Use clear filters to target the right document safely.
Practice
1. What does the
deleteOne method do in MongoDB?easy
Solution
Step 1: Understand the purpose of
ThedeleteOnedeleteOnemethod is designed to remove exactly one document that matches the given filter.Step 2: Compare with other methods
UnlikedeleteMany, which removes multiple documents,deleteOnetargets only one document.Final Answer:
Deletes a single document that matches the filter criteria. -> Option CQuick Check:
deleteOneremoves one matching document [OK]
Hint: Remember: deleteOne removes only one matching document [OK]
Common Mistakes:
- Confusing deleteOne with deleteMany
- Thinking deleteOne updates documents
- Assuming deleteOne deletes all documents
2. Which of the following is the correct syntax to delete one document where the field
name equals "Alice"?easy
Solution
Step 1: Check the filter format
The filter must be an object with key-value pairs, like{name: "Alice"}.Step 2: Validate method call syntax
The correct syntax isdb.collection.deleteOne(filter)where filter is an object.Final Answer:
db.collection.deleteOne({name: "Alice"}) -> Option AQuick Check:
Filter is an object with field and value [OK]
Hint: Use object syntax {field: value} inside deleteOne() [OK]
Common Mistakes:
- Using assignment (=) inside filter
- Passing filter as a string
- Using comparison operators (==) inside filter object
3. Given the collection documents: [{"_id":1, "name":"Bob"}, {"_id":2, "name":"Alice"}, {"_id":3, "name":"Bob"}], what will be the result of
db.collection.deleteOne({name: "Bob"})?medium
Solution
Step 1: Understand deleteOne behavior
deleteOneremoves only one document matching the filter, not all.Step 2: Identify which document is deleted
It deletes the first document it finds withname: "Bob", which is the one with_id:1.Final Answer:
Deletes the first document with name 'Bob' only. -> Option AQuick Check:
deleteOne removes one matching document, not all [OK]
Hint: deleteOne deletes only one matching document, not all [OK]
Common Mistakes:
- Assuming deleteOne deletes all matches
- Thinking it deletes documents with other names
- Believing no document is deleted if multiple matches exist
4. What is wrong with this code snippet?
db.collection.deleteOne({name: 'John')medium
Solution
Step 1: Check the filter object syntax
The filter object starts with '{' but does not have a matching closing '}'.Step 2: Validate quotes and method usage
Single quotes are allowed in JavaScript, and deleteOne is correct for deleting one document.Final Answer:
Missing closing brace '}' in the filter object. -> Option DQuick Check:
Filter objects must have matching braces [OK]
Hint: Check for matching braces in filter objects [OK]
Common Mistakes:
- Ignoring missing braces causing syntax errors
- Confusing single vs double quotes as error
- Thinking deleteOne is wrong method here
5. You want to delete a user document only if the user's
status is "inactive" and age is greater than 30. Which deleteOne filter correctly achieves this?hard
Solution
Step 1: Understand filter conditions
We want to delete a document where both conditions are true:statusis "inactive" ANDageis greater than 30.Step 2: Check filter syntax for AND condition
In MongoDB filters, multiple key-value pairs in the object imply logical AND. Thus,db.collection.deleteOne({status: "inactive", age: {$gt: 30}})is correct.$orwould match if either condition is true.Final Answer:
db.collection.deleteOne({status: "inactive", age: {$gt: 30}}) -> Option BQuick Check:
Multiple fields in filter imply AND [OK]
Hint: Multiple fields in filter object mean AND conditions [OK]
Common Mistakes:
- Using || inside filter object (invalid syntax)
- Using == inside filter object
- Using $or for AND conditions
