Module 3 Assignment: Database Integration
Objective
The objective of this assignment is to integrate MongoDB into the backend system, enabling
persistent user storage. Students will learn how to use Mongoose to define schemas, interact
with the database, and modify existing APIs to work with MongoDB.
Scenario
As the LMS project evolves, it’s essential to persist user data for scalability and reliability. In
this assignment, you will connect the [Link] server to MongoDB and ensure that user data
is stored and retrieved efficiently. This task is critical for creating a robust backend for the
LMS.
Requirements
Step-by-Step Instructions
1. Database Connection
Set Up MongoDB:
o Option 1: Use MongoDB Atlas
Create an account on MongoDB Atlas.
Set up a new cluster and get the connection string.
o Option 2: Use a Local MongoDB Setup
Install MongoDB locally from the official website.
Start the MongoDB service and ensure it's running on
localhost:27017.
Install Required Packages:
o Install mongoose for MongoDB integration:
npm install mongoose
o Install dotenv for managing environment variables:
npm install dotenv
Configure .env File:
o Create a .env file in the project root directory and add the MongoDB
connection string:
MONGO_URI=mongodb+srv://
<username>:<password>@[Link]/<database>?
retryWrites=true&w=majority
o Add .env to .gitignore to exclude it from version control.
Connect to MongoDB:
o Update [Link] to include MongoDB connection logic:
2. Mongoose Schema for Users
Create a User Schema:
o Create a new file named [Link] in the models/ folder.
o Define the Mongoose schema:
3. Update POST /register API
Hash Passwords:
o Install bcrypt for password hashing:
o Update the POST /register route in [Link]:
4. Update GET /user/:id API
Retrieve User Data by ID:
o Update the GET /user/:id route in [Link]:
5. Implement GET /users API
Fetch All Users:
o Add a new route in [Link]:
Recommended Approach
Use MongoDB Atlas for cloud-based storage for easy setup and scalability.
Use bcrypt for secure password hashing.
Include error handling for duplicate email entries and database connection issues.
Deliverables
1. Functional APIs connected to MongoDB:
o POST /register
o GET /user/:id
o GET /users
2. Updated Mongoose schema for users.
3. MongoDB instance containing persisted user data.
Submission Guidelines
1. Files to Submit:
o src/[Link] (updated server with MongoDB integration).
o src/models/[Link] (Mongoose schema for users).
o src/routes/[Link] (updated routes for user management).
o .env file (do not include sensitive data; provide a template with placeholders).
2. Compress Files:
o Place all files and folders into a ZIP file named
Module3_Assignment_<YourName>.zip.
3. Upload:
o Submit the ZIP file to the designated LMS assignment portal.
Evaluation Criteria
1. Database Integration (40%):
o Is the database connection functional and stable?
o Are the APIs correctly interacting with the MongoDB collection?
2. Schema Design and Implementation (30%):
o Does the Mongoose schema include the required fields and validation?
o Is password hashing implemented correctly?
3. API Functionality (20%):
o Are the POST /register, GET /user/:id, and GET /users APIs fully
functional with MongoDB?
4. Code Quality and Error Handling (10%):
o Is the code modular, readable, and well-documented?
o Are errors properly handled and user-friendly messages returned?