MongoDB is one of the most popular NoSQL databases used in modern web applications. Whether you're building a REST API with Node.js, a MERN stack application, or a scalable SaaS platform, understanding CRUD operations is essential.
CRUD stands for:
- Create → Insert data
- Read → Retrieve data
- Update → Modify data
- Delete → Remove data
In this guide, we'll explore MongoDB CRUD operations with practical examples that developers can use in real-world projects.
What Are CRUD Operations?
CRUD operations represent the four basic actions performed on data in a database.
| Operation | Purpose |
|---|---|
| Create | Add new documents |
| Read | Retrieve documents |
| Update | Modify existing documents |
| Delete | Remove documents |
Let's assume we have a collection called users.
Example document:
{
"_id": "665c123456789",
"name": "John Doe",
"email": "john@example.com",
"age": 28,
"status": "active"
}
Create Operations
Create operations insert new documents into a MongoDB collection.
Insert One Document
db.users.insertOne({
name: "John Doe",
email: "john@example.com",
age: 28,
status: "active"
});
Result:
{
"acknowledged": true,
"insertedId": ObjectId("665c123456789")
}
Insert Multiple Documents
db.users.insertMany([
{
name: "Sarah",
email: "sarah@example.com",
age: 25
},
{
name: "Mike",
email: "mike@example.com",
age: 32
}
]);
This is useful when importing datasets or seeding databases.
Read Operations
Read operations retrieve documents from collections.
Retrieve All Documents
db.users.find();
Retrieve One Document
db.users.findOne({
email: "john@example.com"
});
Output:
{
"_id": "665c123456789",
"name": "John Doe",
"email": "john@example.com",
"age": 28
}
Query with Filters
Find users older than 25:
db.users.find({
age: {
$gt: 25
}
});
Query with Multiple Conditions
db.users.find({
age: {
$gte: 18
},
status: "active"
});
Sorting Results
Sort users by age:
db.users.find().sort({
age: 1
});
Ascending:
1
Descending:
-1
Example:
db.users.find().sort({
age: -1
});
Update Operations
Update operations modify existing documents.
Update One Document
db.users.updateOne(
{
email: "john@example.com"
},
{
$set: {
age: 29
}
}
);
Result:
{
"matchedCount": 1,
"modifiedCount": 1
}
Update Multiple Documents
db.users.updateMany(
{
status: "inactive"
},
{
$set: {
status: "active"
}
}
);
Increment a Numeric Value
db.users.updateOne(
{
email: "john@example.com"
},
{
$inc: {
loginCount: 1
}
}
);
This is commonly used for:
- Login counters
- View counters
- Analytics tracking
Delete Operations
Delete operations remove documents from collections.
Delete One Document
db.users.deleteOne({
email: "john@example.com"
});
Result:
{
"deletedCount": 1
}
Delete Multiple Documents
db.users.deleteMany({
status: "inactive"
});
Delete All Documents
db.users.deleteMany({});
⚠️ Use carefully. This removes every document in the collection.
MongoDB CRUD Operations Using Node.js
Most developers interact with MongoDB through Node.js applications.
Example using the MongoDB driver:
const { MongoClient } = require("mongodb");
const client = new MongoClient(MONGO_URI);
await client.connect();
const db = client.db("mydb");
const users = db.collection("users");
await users.insertOne({
name: "John Doe",
email: "john@example.com"
});
Find users:
const result = await users.find().toArray();
console.log(result);
Update user:
await users.updateOne(
{
email: "john@example.com"
},
{
$set: {
age: 30
}
}
);
Delete user:
await users.deleteOne({
email: "john@example.com"
});
Common CRUD Mistakes Developers Make
1. Missing Indexes
Without indexes, MongoDB scans entire collections.
Example:
db.users.createIndex({
email: 1
});
2. Updating Without Filters
Dangerous:
db.users.updateMany(
{},
{
$set: {
status: "active"
}
}
);
This updates every document.
3. Deleting Without Backup
Always verify conditions before running:
deleteMany()
especially in production environments.
Best Practices
Use Indexes
Index frequently queried fields.
db.users.createIndex({
email: 1
});
Validate Data
Use schema validation or Mongoose.
Limit Returned Fields
Instead of:
db.users.find();
Use:
db.users.find(
{},
{
name: 1,
email: 1
}
);
Use Pagination
For large datasets:
db.users.find()
.skip(20)
.limit(10);
When CRUD Operations Are Used in Real Applications
MongoDB CRUD operations power:
- User management systems
- CRM platforms
- E-commerce applications
- SaaS products
- Analytics dashboards
- Social media platforms
Every time a user signs up, updates a profile, views data, or deletes an account, CRUD operations are happening behind the scenes.
Final Thoughts
CRUD operations are the foundation of every MongoDB application.
Once you understand how to create, read, update, and delete documents efficiently, you'll be able to build APIs, dashboards, SaaS platforms, and enterprise applications with confidence.
While the examples in this guide focus on basic operations, mastering CRUD is the first step toward advanced MongoDB concepts such as aggregation pipelines, indexing, transactions, replication, and sharding.
If you're learning MongoDB, make sure CRUD operations become second nature before moving on to more advanced topics.
Top comments (0)