Trusted answers to developer questions

How to connect MongoDB through Mongoose on Node.js

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

MongoDB is a NoSQL database that lets developers send or receive data as JSON documents. Because of the schemaless nature of MongoDB databases, one is prone to make many mistakes when first starting.

Differences between MongoDB and Mongoose

It is always easy to confuse Mongoose and MongoDB in the developer’s world. Both are not the same.

MongoDB is a NoSQL database. It is schemalessno table definition, as seen in SQL databases. Schemaless could also mean that the user defines the database his own way, unlike SQL databases where there is a set schema to follow while working with the database.

MongoDB uses Mongoose, an object data modeling library for MongoDB and Node.js. It is used to manage relationships between data and provide schema validation. It translates those objects in code with the representation of those objects in MongoDB.

In the example below, Mongoose provides connectivity to MongoDB, and just after the connection is established, the schema definition can be carried out. This makes database management easy.

Set up a connection

We would set up a connection between MongoDB and Mongoose as needed when we build a web application on Node.js.

First, Node.js should already be installed on the computer, and one should be able to navigate through the Command Line Interface and native IDE used for development.

One should be in terms of Express.js. There should be a MongoDB instance running on the computer. We can now install the Mongoose library right on the working directory.

Code

To set up a connection, we create a database.js file, and then we could require our mongoose and express files there.

If mongoose and express are not installed, you can install them with the following commands.

$npm install mongoose
$npm install express 

We can now connect the MongoDB through Mongoose.

const express = require ('express');
const mongoose = require ('mongoose');
mongoose.connect('mongodb://localhost:27017/DataApp;
{useNewUrlParsee: true;
useUnifiedTopology:true;
});
.then(()=>{
console.log("CONNECTION OK")
})
.catch(err=>{
console.log("CONNECTION IS BAD")
console.log(err)
});

The code should be saved in the database.js file in the folder where the Mongoose file is being downloaded.

$ node database.js

Overview

The code above creates the database, and one can now define the necessary schemas to be used in the app development.

The localhost port can give an error message if the host number is changed or mistaken with the wrong number.

This connection format ensures that we use a new URL parser from Mongoose and not any deprecated practices.

The .catch method reports to us where there is an error in the connection. It gives a prompt message of "CONNECTION IS BAD" when there is a faulty connection, and it prints the supposed error message through the "console.log(err)" command.

If the connection is good, the prompt "CONNECTION IS OK" is displayed on the terminal. The error messages are specified based on the exact error done by the developer.

Conclusion

As shown here, Mongoose provides connectivity for MongoDB and Node.js in the development of the application database.

RELATED TAGS

mongodb
mongoose
node.js
Did you find this helpful?