MongoDB CRUD Interview Questions
Master MongoDB CRUD Operations with interview-focused questions covering Create, Read, Update, Delete, Query Operators, Projection, Sorting, Pagination, Bulk Operations, Upsert, and Spring Boot examples.
Introduction
CRUD stands for
- Create
- Read
- Update
- Delete
Every MongoDB application performs CRUD operations thousands or even millions of times daily.
Examples include
- User Registration
- Product Search
- Banking Transactions
- Shopping Cart
- Order Management
- Social Media Posts
Understanding CRUD operations is essential for every Backend Developer and is one of the most frequently asked MongoDB interview topics.
MongoDB CRUD Architecture
flowchart LR
Application --> MongoDBDriver --> MongoDB
MongoDB --> Insert
MongoDB --> Find
MongoDB --> Update
MongoDB --> Delete
1. What is CRUD?
Answer
CRUD represents the four basic database operations.
| Operation | MongoDB Method |
|---|---|
| Create | insertOne(), insertMany() |
| Read | find(), findOne() |
| Update | updateOne(), updateMany() |
| Delete | deleteOne(), deleteMany() |
2. What is insertOne()?
Used to insert one document.
Example
db.customers.insertOne({
name:"John",
city:"Austin"
})
3. What is insertMany()?
Inserts multiple documents.
db.customers.insertMany([
{name:"John"},
{name:"Alice"},
{name:"Bob"}
])
Create Flow
flowchart LR
Application --> insertOne() --> Collection --> DocumentStored
4. Difference between insertOne() and insertMany()?
| insertOne() | insertMany() |
|---|---|
| One Document | Multiple Documents |
| Single Insert | Batch Insert |
| Lower Throughput | Better Performance |
5. What is find()?
Returns multiple documents.
db.customers.find()
6. What is findOne()?
Returns only one matching document.
db.customers.findOne({
city:"Austin"
})
7. What is Projection?
Projection returns only selected fields.
db.customers.find(
{},
{name:1,city:1}
)
Projection
flowchart LR
Document --> Projection --> SelectedFields
8. What is updateOne()?
Updates the first matching document.
db.customers.updateOne(
{name:"John"},
{$set:{city:"Dallas"}}
)
9. What is updateMany()?
Updates all matching documents.
db.customers.updateMany(
{city:"Austin"},
{$set:{state:"Texas"}}
)
10. What is replaceOne()?
Replaces the entire document.
db.customers.replaceOne(
{_id:1},
{
name:"Alex",
city:"Houston"
}
)
11. Difference between updateOne() and replaceOne()?
| updateOne() | replaceOne() |
|---|---|
| Updates Fields | Replaces Entire Document |
| Keeps Existing Fields | Removes Missing Fields |
12. What is deleteOne()?
Deletes the first matching document.
db.customers.deleteOne({
name:"John"
})
13. What is deleteMany()?
Deletes multiple documents.
db.customers.deleteMany({
city:"Austin"
})
Delete Flow
flowchart LR
DeleteRequest --> MongoDB --> DocumentRemoved
14. What is Upsert?
Upsert means
Update
OR
Insert
If document exists
↓
Update
Otherwise
↓
Insert
db.customers.updateOne(
{name:"John"},
{$set:{city:"Dallas"}},
{upsert:true}
)
15. What is Bulk Write?
Performs multiple operations in one request.
Supports
- Insert
- Update
- Delete
Improves performance.
16. What is bulkWrite()?
db.customers.bulkWrite([
{
insertOne:{
document:{name:"John"}
}
},
{
updateOne:{
filter:{name:"John"},
update:{$set:{city:"Austin"}}
}
}
])
17. What are Query Operators?
MongoDB provides operators such as
- $eq
- $gt
- $gte
- $lt
- $lte
- $ne
- $in
- $nin
18. Example of Comparison Operators
db.products.find({
price:{
$gt:500
}
})
19. What are Logical Operators?
Supported
- $and
- $or
- $not
- $nor
Example
db.employee.find({
$or:[
{city:"Austin"},
{city:"Dallas"}
]
})
20. What are Array Operators?
Examples
- $all
- $elemMatch
- $size
db.students.find({
skills:"Java"
})
21. What is $set?
Updates specific fields.
{
$set:{
city:"Dallas"
}
}
22. What is $unset?
Removes fields.
{
$unset:{
phone:""
}
}
23. What is $inc?
Increments numeric values.
{
$inc:{
balance:100
}
}
Useful for
- Wallet
- Inventory
- Likes
24. What is $push?
Adds an element to an array.
{
$push:{
skills:"MongoDB"
}
}
25. What is $pull?
Removes an array element.
{
$pull:{
skills:"Java"
}
}
26. What is Sorting?
Ascending
db.employee.find().sort({
salary:1
})
Descending
db.employee.find().sort({
salary:-1
})
27. What is Pagination?
Uses
- skip()
- limit()
Example
db.products.find()
.skip(20)
.limit(10)
Pagination Flow
flowchart LR
Query --> Skip --> Limit --> Result
28. What is Count?
db.employee.countDocuments({
department:"IT"
})
Returns total matching documents.
29. Banking Example
Deposit
db.accounts.updateOne(
{_id:101},
{$inc:{balance:500}}
)
30. E-Commerce Example
Product Search
db.products.find({
category:"Laptop"
})
.sort({
price:1
})
.limit(20)
31. HR Example
Employee Update
db.employee.updateOne(
{_id:100},
{$set:{designation:"Manager"}}
)
32. Logging Example
Delete old logs
db.logs.deleteMany({
year:2023
})
33. Spring Boot Example
@Autowired
private MongoTemplate mongoTemplate;
Employee employee =
mongoTemplate.findById(
"100",
Employee.class
);
Repository Example
public interface EmployeeRepository
extends MongoRepository<Employee,String>{
}
34. Common CRUD Mistakes
- Missing Indexes
- Updating Entire Documents
- Using skip() on huge datasets
- Missing Projections
- Ignoring Bulk Operations
CRUD Workflow
flowchart LR
Create --> Read --> Update --> Delete
Enterprise Best Practices
- Create indexes for frequently searched fields.
- Use Projection to reduce network traffic.
- Use Bulk Operations for batch processing.
- Use Upsert where appropriate.
- Avoid replacing entire documents unnecessarily.
- Prefer pagination over returning large datasets.
- Use indexes for sorting.
- Validate user input.
- Monitor slow queries.
- Test CRUD operations with production-sized datasets.
Quick Revision
| Topic | MongoDB Method |
|---|---|
| Insert One | insertOne() |
| Insert Many | insertMany() |
| Find One | findOne() |
| Find Many | find() |
| Update One | updateOne() |
| Update Many | updateMany() |
| Replace | replaceOne() |
| Delete One | deleteOne() |
| Delete Many | deleteMany() |
| Upsert | updateOne(..., upsert:true) |
| Pagination | skip() + limit() |
| Bulk Processing | bulkWrite() |
Interview Tips
Interviewers frequently ask
- Difference between insertOne() and insertMany().
- Difference between find() and findOne().
- What is Projection?
- What is Upsert?
- Difference between updateOne() and replaceOne().
- Explain $set and $unset.
- Explain $push and $pull.
- How is pagination implemented?
- What are Bulk Operations?
- Show CRUD using Spring Boot.
Always explain that MongoDB CRUD operations are optimized for document-based storage, and features such as projections, bulk operations, update operators, and indexes significantly improve application performance.
Summary
MongoDB CRUD operations provide a rich and efficient API for managing document data. From simple inserts and queries to bulk updates, projections, pagination, and advanced update operators, MongoDB enables developers to build high-performance, scalable applications with minimal complexity.
Understanding CRUD methods, query operators, update operators, bulk processing, and Spring Data MongoDB integration is essential for backend development and MongoDB interviews.