How to Connect Express Application To MongoDB .....

HELLO FRIENDS......Welcome To Technovichar.com. This Post is About How to Connect MongoDB To Express Application Using Mongoose. IF You Don't Know How to Connect MongoDB To Express Application Using Mongoose. So Check Out Full Post. If You Like This Article Content Then Share It With Your Friends.


Basic Introduction of MongoDB :


First We Start with What is MongoDB ??? . MongoDB is a Database. Which is Use in Backend. By Using MongoDB We Can Store Our Website Data. MongoDB is Used with One Backend Language. This Article Explain How to Connect Express Application To MongoDB with Mongoose.    


 


  




Page Content : 


Which Package is Required


Connect with Database


Check Database is Connected or Not


Insert First Data


 Check Data is inserted or Not.

 

Which Package is Required ???


First Of All Make One Folder MONGOOSE_CONNECT (Any Name ) then Inside this Folder Make one app.js File.

One Important Note is Mongodb is Also Required. So Download MongoDB

Install This Packages To MONGOOSE_CONNECT Folder




1) Express 


How to Install Express ?? 

First Download Node js Application Then Run npm install Express Command


2) Mongoose  


To Install Mongoose Run npm install mongoose Command


After Installaling This Two Packages Require mongoose in Node Application

const mongoose = require("mongoose");

 

Then Connect With Dababase:


mongoose.connect("mongodb://localhost:27017/mydatabase",{ useNewUrlParser: true,useUnifiedTopology:true});


Now Check Database is Connected or Not :


mongoose.connect("mongodb://localhost:27017/mydatabase",{ useNewUrlParser: true,useUnifiedTopology:true}).then(()=>{

    console.log("Database is Connected");

})


By Using This Command If You Make Database Which name is mydatabase then data insert into it.but if You can't Make Any Database then it Will Automatically Make Which Name is mydatabase.

Then Make Schema Structure to Insert Value Like name ,  id , address and Many More.


const MySchema = new mongoose.Schema({

    name:String,

     id:Number,

    address:String,

    PhoneNumber:Number

});


By Using mongoose.model make Collection Which Name is mycollegecollection.


const mycollegecollection = new mongoose.model("mycollegeCollection",MySchema);



Also Read This :


How to Make Your Own XML SITEMAP GENERATOR Using Express js..........



Now insert First Student Data :


const collegedata = async()=>{

    const FirstStudent = new mycollegecollection({

        name:"XYZ",

        id:1234567,

        address:"Ahmedabad",

        PhoneNumber:2828283949,

     })

   const result =await FirstStudent.save();

 }

collegedata();


Now Data is Inserted Into Database.



How to Check Data is Inserted or Not :


First Write  Show dbs  To Show ALL Database in MongoDB. Check here mydatabase is Available or Not . if It is Available then Data Will
Inserted. 

Second Step is Check Collections :

Write show collections  To Check All The Collections in Database. Check here mycollegecollection is Available or Not.

Third Step is :

write db.mycollegecollection.find() then All the Inserted Data is  Display.





So This is All About How to Connect Express Application To MongoDB Using Mongoose. If You Like This Article Then Share with Your Friends and Colleagues. 


I HOPE THAT THIS


POST IS


HELPFUL FOR YOU.


THANKYOU FOR READING.




TAGS : MONGOOSE , MONGODB , EXPRESS 




About Author : 

Hello friends, I am Abhi Patel author & founder of Technovichar.com. I am a student of IT engineering. I like to research and write about new things even if it is from any field. You will continue to support us in this way, we will keep on bringing such interesting information for you.

Post a Comment

0 Comments