Using MongoDB On Node.js Application Using Mongoose

Table of contents
  • Introduction
  • Why MongoDB?
  • Source Code
  • Background
  • Setting up Node application
  • Getting the required packages
  • Creating the Node App
  • Setting up database
  • Creating a Mongoose model
  • Setup the data
  • Inserting the data into MongoDB
  • Verify the data
  • To do
  • Conclusion
  • Your turn. What do you think?
Introduction

In this post, we are going to see how we can use MongoDB on 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 in our Node JS application. I hope you will like this article.

You can always see this article on my blog here

Why MongoDB?

Here, I am going to list down the reasons why I had chosen 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.
Source Code

You can always clone or download the source code here

Background

Node JS has become a trend now. One of the recommended databases for a Node application is MongoDB. As we discussed, a MongoDB doesn’t have any structure inbuilt, 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 Node application

To get started with the Node JS, create a folder and name it as per your wish, 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 command below.

  1. npm init
NPM Init

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.

  1. npm init --yes

Let’s review our package.json file.

  1. {  
  2.     "name""node-mongodb-mongoose",  
  3.     "version""1.0.0",  
  4.     "description""A Node application with MongoDB and Mongoose",  
  5.     "main""index.js",  
  6.     "scripts": {  
  7.         "test""echo \"Error: no test specified\" && exit 1"  
  8.     },  
  9.     "keywords": ["Node""Mongoose""MongoDB"],  
  10.     "author""Sibeesh Venu",  
  11.     "license""ISC"  
  12. }  

 

Getting the required packages

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

  1. 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.
  1. 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.

  1. var express = require("express")  
  2. var app = express()  
  3. app.listen("3010", () => {  
  4.     console.log("I just started listening!.")  
  5. })  

 

Now run the command node server.jsand make sure that you are getting a console as ” I just started listening!.”.

If you are getting an error as preceding, 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
events.js:136
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 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 preceding.

mongodb://<dbuser>:<dbpassword>@ds038319.mlab.com:38319/mylearning

We will be updating the connection string with 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.

  1. var conString = "mongodb://admin:[email protected]:38319/mylearning"  
  2. /** 
  3.  * Models 
  4.  */  
  5. var User = mongoose.model("User", {  
  6.     firstName: String,  
  7.     lastName: String  
  8. })  
Please note that, when you move the codes 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.

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

 

Setup the data

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

  1. var dummyUser = {  
  2.     firstName: "Sibeesh",  
  3.     lastName: "Venu"  
  4. }   
Inserting the data into MongoDB

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

  1. mongoose.connect(conString, {  
  2.     useMongoClient: true  
  3. }, () => {  
  4.     console.log("DB is connected")  
  5.     saveData()  
  6. })  
  7.   
  8. function saveData() {  
  9.     var user = new User(dummyUser);  
  10.     user.save();  
  11. }  

 

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 mLab database, here is how my data saved.

MongoDB Data Insertion

MongoDB Data Insertion

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

Todo

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 is connected. In our real life, this is not a recommended way. You must create an Ajax post request and handle the same. I am leaving that task to you. Please try to do that and let me know. I would love to hear back from you.

Conclusion

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

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

Up Next
    Ebook Download
    View all
    Learn
    View all