Share
Explore

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 in the necessary information to create 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 database using MongoDB and Mongoose. Follow these steps to build 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:


const mongoose = require('mongoose');
const studentSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: {
type: Number,
required: true
},
major: {
type: String,
required: true
},
courses: {
type: [String],
required: true
}
});

module.exports = mongoose.model('Student', studentSchema);

2. **Connect to MongoDB** - In our project directory, create a new file called `db.js`. This will be our database connection file. Add the following code to connect to MongoDB:


javascript
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/student-enrollment-system', {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Database connected successfully');
});

3. **Create and Save a Student** - In our project directory, create a new file called `app.js`. This will be our application logic file. Add the following code to create and 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 to send a POST request to `http://localhost:3000/students` with the following JSON data:


json
{
"name": "John Doe",
"age": 25,
"major": "Computer Science",
"courses": ["Math", "Physics", "Computer Science"]
}
```

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:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.