Analytics Vidhya
Published in

Analytics Vidhya

Adding Mongo DB to a simple Web Application created with Node

taken from here

In this article, We will add database to our simple publisher created here.

Following modifications are to be done for our publisher web application,

  1. We need to store the information received from user in database.
  2. We need to display the information to user.

Why Mongo DB ?

Mongo DB is NoSQL database. Real world data is not always available in tabular structure. So, we are going with no sql database.

Mongo DB uses JSON documents to store data. It also supports regular expressions.

We are going to use Mongoose in our website. Mongoose is a tool which make our work with Mongo DB easy.

Install and Import mongoose

Install with following command

npm i mongoose

Import inside app.js

const mongoose = require("mongoose");

Connect to database

We are creating a blogDataBase and connecting to it.

include this in app.js

mongoose.connect("mongodb://localhost:27017/blogDB", {useNewUrlParser: true, useUnifiedTopology: true});

Modifying callbacks in app.js

We are going to add blogDataBase to publisher created in previous article without changing its functionality.

Recall the posts array we used to store content.

let posts=[]
let post = {
title : req.body.title,
postc : req.body.post
};
posts.push(post);

Now we are going to create posts collection inside our blogDataBase.

First we need to create schema for our posts collection

const postSchema = {title: String,content: String};

Now create a collection

const Posts = mongoose.model("Posts", postSchema);

We have created collection. We will add documents to our collection when user writes post. Modify callback for “/compose” route.

app.post("/compose",function(req,res){

const post = new Posts ({
title: req.body.title, content: req.body.post });
post.save();
res.redirect("/");
});

‘post’ is a document saved inside Posts collection.

Done with adding user input to database. Now, we retrieve data from database inside home page and post page.

Call back of home page is modified as follows

app.get("/",function(req,res)
{
Post.find({}, function(err, posts){
console.log(posts);
res.render("home", {
startingContent: homeStartingContent,posts: posts});
});
}
);

Post.find(<condition>,function(err, <result>){…}) is syntax for finding documents inside collection.

Similar to this, there is findOne function to retrieve a document from collection based on a condition. We are going to use this inside post callback.

app.get("/posts/:postname",function(req,res){

Post.findOne({title: req.params.postname}, function(err, post){
if(!err)
{
res.render("post", {
title: post.title,content: post.content});}
else
{
console.log(err);
}
});
});

That’s it !!!

Caution

You may get error while starting mongoDB in mac with “mongod” command.

Try following command instead

sudo mongod --dbpath /System/Volumes/Data/data/db

Conclusion

Without modifying any code of ejs files, we have added database to our publisher website created here.

Complete code can be found here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store