0

How to Connect Mongoose with Express Application

In this article, we will see how to connect MongoDB with Node.js using ORM called Mongoose. How to Connect Mongoose with Express Application

Implementing Twilio with Node.js

Mongoose is a ODM (Object Document Mapping) for the MongoDB database. Mongoose ease the process of Connecting and managing the MongoDB and Node.js

what is Mongoose?

Mongoose is an ODM used to establish a connection with the MongoDB database. Above all, it provides schema structure for the database collection(In MySQL, it is called as Table)

How Mongoose Works?

  • Express App do the Object mapping with Mongoose and Mongoose access the MongoDB through Mongo Drive
  • we can also use the Mongo Driver directly from Node.js

Express,Mongoose Project Setup

First of all, install express and mongoose.

npm init -yes
npm install mongoose express body-parser 

Create a file name called app.js.

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");

const app = express();

const PORT = 3000;

app.listen(PORT,() => {
    console.log(`app is listening to PORT ${PORT}`);
})

now start the server with the command node app.js and you should see some message in the console as app is listening to PORT 3000

Further, we need to connect MongoDB database using mongoose. Meanwhile, refer to this official documentation Mongoose

That is to say, connect mongoose to local MongoDB instance with db name as testdb

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

mongoose.connection.on("error",(err)=>{

    console.log("err",err);

});

mongoose.connection.on("connected",(err,res) => {
    console.log("mongoose is connected");
});

Final version of code should look like these. In conclusion, when we run the server with command node app.js . if we can see some message as mongoose is connected. then mongoose connection is established successfully.

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");

const app = express();

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

mongoose.connection.on("error",(err)=>{

    console.log("err",err);

});

mongoose.connection.on("connected",(err,res) => {
    console.log("mongoose is connected");
});

const PORT = 3000;

app.listen(PORT,() => {
    console.log(`app is listening to PORT ${PORT}`);
})

Mongoose handles everything as Schema. we need to design the schema for a Database Collection and we need to set the datatype that the collection is going to have.

For Example, if we are write a schema for an User. we can design it to have username, email and password

const Mongoose = require('mongoose')

const userSchema = new Mongoose.Schema({
  username : {
      type : String,
      required : true
   },
email : {
    type : String
  },
   password : {
    type : String
  }
})

Mongoose.model('User',userSchema);

ganeshmani009

Leave a Reply

Your e-mail address will not be published. Required fields are marked *