MongoDB on Azure? 

Yes, you can run a VM on Azure and host a MongoDB but I hate managing VMs. Luckily Azure has a no-SQL offering called DocumentDB and they currently have a preview of it running that offers protocol support for MongoDB.

What does that mean for Node.js? Well… you code it just like you were working with a MongoDB only it is actually Azure’s DocumentDB offering. Yea!

Creating an Azure  DocumentDB 

On your Azure portal click New and search for DocumentDB, you should find the one with support for MongoDB titled “DocumentDB – Protocol Support for MongoDB (preview)”


Creating test Database & Collections

Once the DocumentDB service has been created you can create a Database by clicking on “Add Database” at the top.


After that click on the newly created Database and click on “Add Collection” at the top of the blade.

CaptureThis is where the pricing comes in when you add the collection, it is based on Request Units so I dropped mine down to 400 for testing.

Connecting to your Database from Node

We now need to get the connection info, go to the first blade in the DocumentDB service and click on settings. You will see the connection info HOST, PORT, USERNAME & PASSWORD.

Use the code snippet below and place these settings in the correct locations.
Notes: do not use “mongodb://” this will not work, use exactly what the HOST reads nothing more and
SSL is required.

var mongo = require('mongodb');

var server = new mongo.Server('HOST', PORT, { ssl: true });
var db = new mongo.Db('DATABASE NAME', server, { w: 1 });
var auth = { user: 'USERNAME', pass: 'PASSWORD' }; (err, db) {
   if (err) return console.log("error opening", err);

   db.authenticate(auth.user, auth.pass, function(err, result) {
      if (err) return console.log("error authenticating", err);

      console.log("authed?", result);

      db.collection('COLLECTION NAME').count(function(err, count) {
         if (err) return console.log("error counting", err);

         console.log("count", count);