Deep Dive into Node.js and MongoDB - Day 2


As mentioned in yesterday's post and continuing my deep dive, tonight I will be deep diving into MongoDB specifically in conjunction with Node.js.


You will need the following:
  1. MongoDB (3.2.9 was the latest available at the time of this writing)
  2. Run npm install mongojs in your Node.js project folder

MongoDB Deep-Dive

A few years back I read the great book, Seven Databases in Seven Weeks in addition to attending a presentation from David Makogon at a local .NET meetup a couple years ago that introduced me to the world of NoSQL. Having spent my career (and pre-career) in a "traditional" relational database I found both sources of information fascinating, in particular the Neo4j graph database. Anyone who has ever done relationship trees in SQL Server/Oracle/MySQL knows how much of a pain it becomes at more than 2 levels deep.

In MongoDB specifically, the idea is to think in collections which are similar to Tables in a relational database. To get started, we'll need to start MongoDB. This may not be the best approach, but this will work. Simply goto C:\Program Files\MongoDB\Server\3.2\bin in a command prompt and type mongod. Upon executing that command, you will get a screenshot similiar to below:

Afterwards we will need to make our "collection", in my case I called mine day2innode with the mongo day2innode command, afterwards you should get a similiar output like below:

From there if you do a show collections command it should return nothing as the "table" is empty (effectively doing a SELECT * FROM Sys.Tables like in SQL Server in my understanding). In the screenshot above I inserted data into the posts collection and then returned it back to confirm it writing successfully..

So now we need to write some Node.js code to write into our new MongoDB. I found using the MongoJS module extremely helpful. Taking the basis of what I had done last night, adding an argument to the route and then writing that to MongoDB was a good first step I thought. So let us dive into the code:

var dburl = 'localhost/day2innode';
var collections = ['posts'];

var express = require('express');
var mongojs = require('mongojs');

var app = express();

    function (req, res) {
        var db = mongojs(dburl, collections);

        var id = req.param('id');

        var postData = db.collection('posts');

        var newData = { 'id': id, 'likes': 2 };

        postData.insert(newData, function(err, post) {
            if (err) {
                return res.json({ message: err });


            return res.json({ message: true });


As you can see a few new additions to the code base all pretty much related to MongoJS.


Seeing as how WebAPI is traditionally paired to a relational database (mainly SQL Server), but knowing that SQL Server 2016 added in JSON Storage I wanted to do a head to head comparison. For the sake of tonight however, I will only post the NodeJS + MongoDB performance metrics tonight. Tomorrow the head to head comparison and any tweaks I learn in between as far as MongoDB goes.

Again all benchmarks were run on my i7-6700k (8x4ghz) and 16gb of DDR4 running @ 3200mhz all on Windows 10 Anniversary Edition. Since we're now dealing with storage, I am running it on a Samsung MZHPV512HDGL M2 PCIe drive.

Running an updated WPF test app, the results were pretty interesting:

Writing 5120 "rows" in 5120 separate calls in 19 seconds isn't bad especially in its unoptimized state. It will be interesting to compare it tomorrow night to WebAPI/SQL Server 2016.

Next up...

Next up as mentioned above, adding in SQL Server 2016/WebAPI comparisons and if time permits tomorrow night after work more deep diving into optimizing the MongoDB/Node.js code. Just in case, all of the code thus far is committed on GitHub.
none on this post