Using MongoDB on Node.js Application Using Mongoose

DZone 's Guide to

Using MongoDB on Node.js Application Using Mongoose

In this post, we are going to see how we can use a MongoDB database with our Node.js application with the help of the package Mongoose.

· Web Dev Zone ·
Free Resource


In this post, we are going to see how we can use MongoDB in our Node.js application with the help of the package Mongoose. We will also be covering some facts about MongoDB so that, as a reader, you will understand why we have chosen MongoDB as our backend. We will be going through some steps to install the required packages using a node package manager terminal, so please bear with me. At the end of this article, I guarantee that you will be proficient on how to connect a MongoDB database to your Node application. I hope you will like this article.

Why MongoDB?

Here, I am going to list the reasons why I chose MongoDB.

  1. MongoDB is a NoSQL database - when I say NoSQL, it means that it doesn't have any structure to be followed.
  2. There are no relationships, we can perform the relation using separate packages like Mongoose.
  3. Everything is JSON, even the data and collections are stored as JSON.
  4. Since the data is stored as JSON, no more conversions are needed. We can directly use it in our application.

You can always clone or download the source code here.


Node.js has become a trend now. One of the recommended databases for a Node application is MongoDB. As we discussed, a MongoDB database doesn't have any structure built-in, here we are going to use a package called Mongoose, with that we can create some structure for our database. Let's just skip the talking and start developing.

Setting Up a Node Application

To get started with Node.js, create a folder and name it; this is going to be our project directory. Here, I am going to use one of my favorite editors, Visual Studio Code. Now please point to your project container, and run the below command.

npm init

This will create a file named "Package.json" in your directory. We will be adding all of our dependencies in this file very soon. The command may ask you some default questions which you need to answer. If you need to create the package.json file with default values in it, you may consider running the below command.

npm init --yes

Let's review our package.json file.

  "name": "node-mongodb-mongoose",
  "version": "1.0.0",
  "description": "A Node application with MongoDB and Mongoose",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "keywords": [
  "author": "Sibeesh Venu",
  "license": "ISC"

Getting the Required Packages

Now that we have the application ready, let's get the required packages ready.

npm install --save mongoose

This will create a new folder, npm_modules, on your project directory where you can see the package Mongoose in it.

npm install --save express

The above command will add the package express to our application.

Creating the Node App

Now we can create our server.js file where we are going to write most of our application code.

var express = require("express")
var app = express()
    console.log("I just started listening!.")

Now run the command node server.js and make sure that you are getting the message, "I just started listening!"

If you are getting the following error, please make sure that you are running the application on an unused port.

PS F:\Visual Studio\Node.JS\Node-MongoDB-Mongoose> node server.js
throw er; // Unhandled 'error' event

Error: listen EADDRINUSE :::3000
at Object._errnoException (util.js:1031:13)
at _exceptionWithHostPort (util.js:1052:20)
at Server.setupListenHandle [as _listen2] (net.js:1367:14)
at listenInCluster (net.js:1408:12)
at Server.listen (net.js:1496:7)
at Function.listen (F:\Visual Studio\Node.JS\Node-MongoDB-Mongoose\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (F:\Visual Studio\Node.JS\Node-MongoDB-Mongoose\server.js:3:5)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)"

Setting Up the Database

Now that we have our application ready, let's go and create our database. I am going to use mLab for creating the database. If you have not installed MongoDB on your machine, I strongly recommend creating a database in mLab. I had created a database there and have got my connection string as follows.


We will be updating the connection string with an actual DB user and password later. Please make sure that you are creating a user for your DB and remember the password.

Creating a Mongoose Model

Let's create a Mongoose model and set up our connection string now, which is going to be our collection. No worries, it is just a JavaScript model.

var conString = "mongodb://admin:admin@ds038319.mlab.com:38319/mylearning"

 * Models 
var User = mongoose.model("User", {
    firstName: String,
    lastName: String

Please note that when you move the code to production, make sure that your password and user fields on the connection string are separated from this file and encrypted.

Now let's connect our database.

mongoose.connect(conString, { useMongoClient: true }, () => {
    console.log("DB is connected")

Setup the Data

We have our model ready, what else is needed now? Yes, you are right, we need data.

var dummyUser = {
    firstName: "Sibeesh",
    lastName: "Venu"

Inserting the Data Into MongoDB

We have got everything ready, so we can insert the model now. Let's do the saving logic first.

mongoose.connect(conString, { useMongoClient: true }, () => {
    console.log("DB is connected")
function saveData() {
    var user = new User(dummyUser);

Verify the Data

Once you run your application, go to your database and check for the entries there. I am sure there will be a new collection ("User") and our inserted data. As I am using a mLab database, here is how my data saved.

Image title

If you have noticed, you can see there is another property names "_id" in our database collection. This is a unique id generated by MongoDB for us, so no need to think about it.

As the purpose of this article is to show how we can insert a data to MongoDB, I called the save method right after the database was connected. In real life, this is not a recommended method. You must create an Ajax post request and handle the event. I am leaving that task to you. Please try to do that and let me know. I would love to hear back from you.


Thanks a lot for reading. Did I miss anything that you think is needed? Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

Kindest Regards
Sibeesh Venu

mongodb, node.js, web application development, web dev

Published at DZone with permission of Sibeesh Venu , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}