DEV Community

Robert Look
Robert Look

Posted on

How To Connect Mongoose (MongoDB) With Express Application

MongoDB is an ODM (Object Document Mapping) for databases. MongoDB makes it easy to Connecting and managing node.js applications.

what is Mongoose?
Mongoose(MongoDB) is a document database with scalability and flexibility. Mongoose is an ODM used to establish a connection with the MongoDB database in node.js. it provides schema structure for the database collection. MySQL it is called as table.

How To Connect Mongoose (MongoDB) With Express Application

const express = require("express")
const Mongoose = require("mongoose")
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(`Example app listening at http://localhost:${port}`)
})
Enter fullscreen mode Exit fullscreen mode

Source: https://www.phpcodingstuff.com/blog/how-to-connect-mongoose-with-express-application.html

Top comments (0)