Skip to content

Latest commit

 

History

History
36 lines (26 loc) · 1.16 KB

studentDatabase.md

File metadata and controls

36 lines (26 loc) · 1.16 KB

Creating Student Database in MongoDB

Pre-requisites

Install MongoDB and mongoose in your project.

Spin up your mongo server and your mongo shell in the terminal.

Steps

  1. Step 1: Require Mongoose in your project

const mongoose= require(“mongoose”);

  1. Step 2: Connect your app with a MongoDB database using the URL where your MongoDB is hosted locally.

mongoose.connect('mongodb://localhost:27017/Student', { useNewUrlParser : true });

  1. Step 3: Create a new schema “studentsSchema”

const studentsSchema={
name:String,
id: Number
};

  1. Step 4: Create a new mongoose model based on this schema using the singular form of your collection i.e. student, and the schema

const Item=mongoose.model(" Student ", studentsSchema);

  1. Step 5: Create a new mongoose document using your Student model

const student1=new Student({
name : " Stu-1 ",
id: 1
});

  1. Step 6: Save that document to your database

student1.save(function (err, student) {
if (err) return console.error(err);
});

Ta-daa, you have now created a Student Database with data in it 🎉 🎉