MERN STACK
Task 3: CRUD Operations with MongoDB
Objective
Learn and practice CRUD operations (Create, Read, Update, Delete) to manage and
manipulate data effectively in a MongoDB collection, a fundamental skill for MERN stack
applications.
Steps to Follow
1. Create a Collection
● Setup:
○ Use MongoDB Compass, Mongo Shell, or programmatically via [Link] to create
a database and a collection.
○ Example database name: mern_app.
○ Example collection name: users.
● Sample Data:
○ Insert initial records with fields like name, email, age, and role.
● Document Structure:
○ Define the schema, even if MongoDB doesn’t enforce it, to maintain consistency:
■ Example: { name: "John Doe", email: "john@[Link]",
age: 30, role: "admin" }
2. Retrieve (Read) Data
● Fetch All Records:
○ Retrieve all documents in the collection to confirm the data exists.
● Query Specific Data:
Main Flow Services and Technologies Pvt. Ltd.
Contact Us. +91 9389641586, +91 97736 99074
Email-Add. [Link]@[Link]
[Link]
○ Filter based on criteria like age > 25 or role = "admin".
○ Use projections to fetch specific fields like name and email without fetching the
entire document.
● Sorting and Pagination:
○ Implement sorting by fields (e.g., sort by age in descending order).
○ Use limit and skip for paginated results.
3. Update Data
● Single Record:
○ Update fields for a specific document, such as changing a user’s role from
"user" to "admin".
● Multiple Records:
○ Apply updates to multiple documents matching a condition, e.g., increase age for
all users by 1.
● Verification:
○ After performing updates, retrieve the modified records to confirm the changes.
4. Delete Data
● Single Record Deletion:
○ Remove a document based on a unique identifier like _id or a specific condition.
● Multiple Records:
○ Delete multiple documents meeting criteria, e.g., remove all users with age >
50.
● Drop Collection:
○ If the collection is no longer needed, drop it completely to free up resources.
○ Caution: Ensure you back up any critical data before performing this step.
Deliverables
Main Flow Services and Technologies Pvt. Ltd.
Contact Us. +91 9389641586, +91 97736 99074
Email-Add. [Link]@[Link]
[Link]
1. Database and Collection:
○ Properly initialized collection with sample data.
2. CRUD Operations:
○ Verified functionality of Create, Read, Update, and Delete actions.
3. Documentation:
○ Outline of the CRUD process.
○ Screenshots of MongoDB Compass or Shell outputs demonstrating each
operation.
4. Insights:
○ Key takeaways from practicing CRUD operations.
Expected Outcomes
● Gain hands-on experience in database management with MongoDB.
● Understand how CRUD operations integrate with MERN stack applications.
● Learn the importance of efficient data manipulation and verification.
Deadline Compliance
● Restriction: Submit the project within 7 days from the start date.
● Reason: Meeting deadlines is crucial in the real-world software development
environment. This restriction helps students practice time management and task
prioritization. In professional settings, tight deadlines are often the norm, and learning
to meet them without compromising quality is an essential skill.
● Learning Outcome: Students will learn to manage their time effectively, complete
projects under pressure, and deliver results on time, which are all important skills in
the workplace.
Main Flow Services and Technologies Pvt. Ltd.
Contact Us. +91 9389641586, +91 97736 99074
Email-Add. [Link]@[Link]
[Link]