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.

1npm init -yes
2npm install mongoose express body-parser

Create a file name called app.js.

1const express = require("express")
2const mongoose = require("mongoose")
3const bodyParser = require("body-parser")
5const app = express()
7const PORT = 3000
9app.listen(PORT, () => {
10 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

1mongoose.connect("mongodb://localhost:27017/testdb", {
2 useNewUrlParser: "true",
5mongoose.connection.on("error", err => {
6 console.log("err", err)
9mongoose.connection.on("connected", (err, res) => {
10 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.

1const express = require("express")
2const mongoose = require("mongoose")
3const bodyParser = require("body-parser")
5const app = express()
7mongoose.connect("mongodb://localhost:27017/testdb", {
8 useNewUrlParser: "true",
11mongoose.connection.on("error", err => {
12 console.log("err", err)
15mongoose.connection.on("connected", (err, res) => {
16 console.log("mongoose is connected")
19const PORT = 3000
21app.listen(PORT, () => {
22 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

1const Mongoose = require("mongoose")
3const userSchema = new Mongoose.Schema({
4 username: {
5 type: String,
6 required: true,
7 },
8 email: {
9 type: String,
10 },
11 password: {
12 type: String,
13 },
16Mongoose.model("User", userSchema)

To Read More

Kubernetes for Nodejs developers

Do you keep hearing the word kubernetes in the tech community and you couldn't u...

TypeScript Interfaces vs Types

In this article, we will see what are interfaces and types and the difference be...

How to find project ideas to practi...

Ever wondered what how to get a real world experience on web development while w...

Building a Production-grade Nodejs,...

This article is the first part of building a production grade nodejs,graphql and...

Modern React Redux Tutorials with R...

This tutorial explain how you can build an application using modern react redux ...

Building a Piano with React Hooks

In this article, we will see how to build a piano with react hooks. Building a P...

TypeScript Basics - The Definitive ...

In this article, we will learn some basics of typescript which helps you to deve...

Here's why podman is more secured t...

In this article we will see about podman and why it is more secured way to run c...

What is gRPC ? How to implement gRP...

Everyone talks about gRPC. Have you ever wonder how it works or how to implement...