Lesson 10 of 50 – Introduction to CRUD
20%

Introduction to CRUD in MongoDB

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.

1. What Is CRUD?

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.

2. Prepare the Database

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.

3. Create Operation - Insert Documents

The Create operation adds new documents to a collection. MongoDB provides methods such as insertOne() and insertMany().

Example 1: Insert One Student
db.students.insertOne({
    name: "Rahul",
    age: 20,
    course: "ADCA",
    city: "Aurangabad"
})

This command inserts one student document into the students collection.

Example 2: Insert Multiple Students
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.

4. Read Operation - Retrieve Documents

The Read operation retrieves documents from a collection. MongoDB provides the find() and findOne() methods.

Example 1: Display All Students
db.students.find()
Example 2: Display One Student
db.students.findOne({
    name: "Rahul"
})
Example 3: Find Students by Course
db.students.find({
    course: "Python"
})
Example 4: Find Students by City
db.students.find({
    city: "Aurangabad"
})

Result: MongoDB displays documents that match the specified query.

5. Update Operation - Modify Documents

The Update operation modifies existing documents. The updateOne() method updates the first matching document.

Example 1: Update a Student's Age
db.students.updateOne(
    { name: "Rahul" },
    { $set: { age: 21 } }
)

The age of the first student named Rahul is changed to 21.

Example 2: Update a Student's Course
db.students.updateOne(
    { name: "Amit" },
    { $set: { course: "Python" } }
)
Example 3: Update Multiple Documents
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.

6. Delete Operation - Remove Documents

The Delete operation removes documents from a collection. MongoDB provides deleteOne() and deleteMany().

Example 1: Delete One Student
db.students.deleteOne({
    name: "Rahul"
})

This removes the first document matching the name Rahul.

Example 2: Delete Multiple Students
db.students.deleteMany({
    status: "Inactive"
})

This removes all documents whose status is Inactive.

Warning: Always check the filter before executing a delete command. An incorrect filter can delete unintended documents. Use a practice database while learning.

7. Complete CRUD Example

The following example demonstrates all four CRUD operations using one student document.

Step 1: Create
db.students.insertOne({
    name: "Suman",
    age: 20,
    course: "JavaScript"
})
Step 2: Read
db.students.find({
    name: "Suman"
})
Step 3: Update
db.students.updateOne(
    { name: "Suman" },
    { $set: { age: 21 } }
)
Step 4: Read the Updated Document
db.students.findOne({
    name: "Suman"
})
Step 5: Delete
db.students.deleteOne({
    name: "Suman"
})
Step 6: Verify Deletion
db.students.find({
    name: "Suman"
})

If no matching document remains, the final query returns no document.

8. CRUD Methods Quick Reference

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.

📝 Practical Assignment

Create a database named college and a collection named students. Complete these tasks using MongoDB Shell:

  1. Insert five student documents.
  2. Include name, age, course, and city fields.
  3. Display all student records.
  4. Find students enrolled in Python.
  5. Find one student by name.
  6. Update one student's age.
  7. Change a student's course.
  8. Delete one student by name.
  9. Display all remaining student documents.

Challenge: Use updateMany() to add a field named status with the value "Active" to all students in your practice collection.

🧠 Quick Quiz

1. What does CRUD stand for?


2. Which method retrieves documents?


3. Which method updates one matching document?