MONGO DB Build the College Enrollment System [MONGO_CES]
# Building a MongoDB Database in Node.js with Mongoose
Welcome students, Let's dive into the world of building a MongoDB database in Node.js with Mongoose.
In this lab, we will be modeling a student enrollment system using the power of MongoDB and Node.js.
MONGOOSE is an NPMJS.com package to programmatically manipulate your MONGO DB.
To get started, let's set up our environment.
## Setting up the Environment
Before we begin, make sure you have Node.js and MongoDB installed on your machine. If you don't have them installed, follow these instructions:
1.**Installing Node.js** - Head over to the official Node.js website and download the latest version of Node.js for your operating system. Follow the installation instructions provided for your operating system.
2.**Installing MongoDB**
Head over to the official MongoDB website and download the latest version of MongoDB for your operating system. Follow the installation instructions provided for your operating system. Make sure to start the MongoDB server after installation.
## Creating a New Project
Now that we have our environment set up, let's create a new project. Open up your command prompt or terminal and navigate to the directory where you want to create your project. Run the following command to create a new Node.js project:
npm init
Follow the prompts and fill inthe necessary information tocreate your project. Once you have created your project, install the following dependencies:
npm install express mongoose
## Building the Database
Now that we have our project set up, let's build our databaseusing MongoDB and Mongoose. Follow these steps tobuild our student enrollment system:
1. **Define the Schema** - In our project directory, create a new folder called `models`.
Inside this folder, create a new file called `student.js`. This will be our schema definition file. Add the following code to define our student schema:
2. **Connect to MongoDB** - In our project directory, createanewfile called `db.js`. This will be our database connection file. Add the following code to connect to MongoDB:
3. **Create and Save a Student** - In our project directory, createanewfile called `app.js`. This will be our application logic file. Add the following code tocreateand save a student:
javascript
const express = require('express');
const Student = require('./models/student');
const db = require('./db');
const app = express();
app.post('/students', async (req, res) => {
const { name, age, major, courses } = req.body;
const student = new Student({ name, age, major, courses });
await student.save();
res.json(student);
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
4. **Test the Application** - Start your MongoDB server by running the following command:
mongod
Then, start your Node.js server by running the following command:
node app.js
Use a tool like Postman tosenda POST request to `http://localhost:3000/students` withthe following JSON data:
Congratulations! You have successfully built a MongoDB database in Node.js with Mongoose to model a student enrollment system.
Conclusion
In this lab, we have learned how to build a MongoDB database in Node.js with Mongoose to model a student enrollment system.
We have covered how to set up our environment, create a new project, define the schema, connect to MongoDB, create and save a student, and test the application.
Hopefully, this lab has given you a solid foundation to build upon in your future projects. Happy learning!
Continue to the more advanced formulation of this Lab by clicking below: