latest posts


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(); app.get('/api/Test', 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) { db.close(); return res.json({ message: err }); } db.close(); return res.json({ message: true }); }); }); app.listen(1338);

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.
Over the weekend I decided to finally start a deep dive into Node.js, specificially for a REST API. Coming from a background in C# based SOAP and REST Web Services using ASP.NET I figured this would be the target of my deep dive into node.js.


To get started I downloaded node.js v6.5 Current for Windows (x64). In addition I installed the Node.js Tools 1.2 for Visual Studio 2015, available here. You will also as indicated on the download link need Visual Studio 2015 with Update 3 RTM or higher (I used Update 3 for those curious).

Node.js Deep-Dive

In watching the MVA on Jump Starting Node.js, I started investigating the Express framework for Node.js seeing this as something similar to how WebAPI was to MVC (before the unification recently).

Code wise for this first deep dive I wanted to keep it simple with a GET request that returns the current date time:

var express = require('express'); var app = express(); app.get('/api/Test', function(req, res) { res.json({ message: new Date().toLocaleString() }); }); app.listen(1338);

Pretty simple, include the express module and then define the route to listen in on like you would with AttributeRouting in WebAPI.

Benchmark Setup

To start off, all of the code is in a new github repo DaysInNode. Each day I will make a new folder to keep the tests separate for historical purposes.

Benchmarking wise I wrote a little WPF app utilizing HttpClient to create several GET requests from 10 to 5260 - knowing this is not a 100% true test, again wanted to keep it simple for Day 1.

All benchmarks were run on my i7-6700k (8x4ghz) and 16gb of DDR4 running @ 3200mhz all on Windows 10 Anniversary Edition.

Benchmark Results

To my surprise Node.js was 3X faster than WebAPI (.NET Core) and in some cases even more so as shown in the graph below. In addition memory usage never exceeded 24.8mb while using IIS Express/WebAPI utilized 3.4mb and Kestrel/WebAPI utilized 135mb.

Next up...

Next up in testing is using a NoSQL database on both ends to test a more realistic scenario such as recording a "Like" or "Dislike".