CRUD is one of the most important concepts in database management. It represents four basic operations: Create, Read, Update, and Delete. These operations allow us to insert new documents, retrieve existing documents, modify information, and remove unwanted documents in MongoDB.
In this lesson, we will understand each CRUD operation using a practical example of a student management database.
CRUD is an abbreviation for four basic database operations:
| Operation | Meaning | Purpose |
|---|---|---|
| C - Create | Insert data | Add new documents. |
| R - Read | Retrieve data | Display stored documents. |
| U - Update | Modify data | Change existing document values. |
| D - Delete | Remove data | Delete unwanted documents. |
Example: In a student management system, CRUD operations help you register students, view their details, update their courses, and delete records when necessary.
Open MongoDB Shell (mongosh) and select a database named
school.
use school
Create a collection named students if it
does not already exist.
db.createCollection("students")
If the collection already exists, MongoDB will report that it already exists. You can continue using it.
The Create operation adds new documents to a collection.
MongoDB provides methods such as
insertOne() and insertMany().
db.students.insertOne({
name: "Rahul",
age: 20,
course: "ADCA",
city: "Aurangabad"
})
This command inserts one student document into the
students collection.
db.students.insertMany([
{
name: "Amit",
age: 21,
course: "Tally Prime",
city: "Patna"
},
{
name: "Priya",
age: 19,
course: "Web Development",
city: "Gaya"
},
{
name: "Neha",
age: 22,
course: "Python",
city: "Aurangabad"
}
])
Result: New student documents are added to the collection.
The Read operation retrieves documents from a collection.
MongoDB provides the find() and
findOne() methods.
db.students.find()
db.students.findOne({
name: "Rahul"
})
db.students.find({
course: "Python"
})
db.students.find({
city: "Aurangabad"
})
Result: MongoDB displays documents that match the specified query.
The Update operation modifies existing documents.
The updateOne() method updates the first
matching document.
db.students.updateOne(
{ name: "Rahul" },
{ $set: { age: 21 } }
)
The age of the first student named Rahul is changed to 21.
db.students.updateOne(
{ name: "Amit" },
{ $set: { course: "Python" } }
)
db.students.updateMany(
{ city: "Aurangabad" },
{ $set: { status: "Active" } }
)
Use updateMany() when you want to modify
every document matching the filter.
Result: The specified field values are updated in the matching documents.
The Delete operation removes documents from a collection.
MongoDB provides deleteOne() and
deleteMany().
db.students.deleteOne({
name: "Rahul"
})
This removes the first document matching the name Rahul.
db.students.deleteMany({
status: "Inactive"
})
This removes all documents whose status is Inactive.
The following example demonstrates all four CRUD operations using one student document.
db.students.insertOne({
name: "Suman",
age: 20,
course: "JavaScript"
})
db.students.find({
name: "Suman"
})
db.students.updateOne(
{ name: "Suman" },
{ $set: { age: 21 } }
)
db.students.findOne({
name: "Suman"
})
db.students.deleteOne({
name: "Suman"
})
db.students.find({
name: "Suman"
})
If no matching document remains, the final query returns no document.
| CRUD Operation | MongoDB Method | Purpose |
|---|---|---|
| Create | insertOne() |
Insert one document. |
| Create | insertMany() |
Insert multiple documents. |
| Read | find() |
Retrieve matching documents. |
| Read | findOne() |
Retrieve one matching document. |
| Update | updateOne() |
Update one matching document. |
| Update | updateMany() |
Update all matching documents. |
| Delete | deleteOne() |
Delete one matching document. |
| Delete | deleteMany() |
Delete all matching documents. |
Create a database named college and a
collection named students. Complete these
tasks using MongoDB Shell:
Challenge: Use
updateMany() to add a field named
status with the value
"Active" to all students in your practice
collection.
1. What does CRUD stand for?
2. Which method retrieves documents?
3. Which method updates one matching document?