Header Ads

How to Use Mongoose to Connect with MongoDB using Nodejs

Mongoose is the most essential plugin for dealing with MongoDB NoSQL database. With  Mongoose, We can perform the several CRUD operations with few lines of codes.

Mongoose gives a straight-forward, composition based answer for the model your application information. It incorporates worked in sort throwing, approval, question building, business rationale snares, and the sky is the limit from there, out of the container.

In basic words, Mongoose goes about as a transitional among MongoDB and a server-side language(like NodeJs)

How to Connect MongoDB with Mongoose?



  • Setup your nodejs application project.

  • Run this following commands using cmd.


npm install mongoose


  • Now create a new file named index.js.

  • and paste the following code mentioned below.


const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/ganesha')
.then(() => console.log('connected to mongodb'))
.catch(err => console.log('err to connect'+err));


  • Run your server using PowerShell.


node index.js

Output
// connected to mongodb

Conclusion


Here how you can config your MongoDB server using mongoose in nodejs.

No comments