Atlas Driver Compatibility and Testing for Node.js
Jay Gordon is a Technical Account Manager with MongoDB and is available via our chat to discuss MongoDB Cloud Products at https://cloud.mongodb.com.
MongoDB was excited to announce the introduction of our Database as a Service, Atlas. Atlas lets you put aside your concerns with building your database’s infrastructure and focus on building your application.
One of the more important parts of developing your application using the driver that fits with the version of MongoDB. Today we’re going to build a tiny Node.js application to validate that our driver works, that we’re able to authenticate and validate we’re ready to start building our data.
Let’s get started by building a very basic MongoDB Atlas Cluster. For this exercise we just require an M10 class, which is both our most modestly priced and best for low end use. Navigate your browser to https://cloud.mongodb.com and sign up for an account.
As you can see, we are building a new cluster that’s using MongoDB 3.2 along with the WiredTiger storage engine. These are our defaults for you to work with as you determine the needs for your application from a development standpoint. Let’s check our compatibility with NodeJS and this version of MongoDB by navigating to our NodeJS driver page.
Now let’s validate my local environment, I used npm
to install this package so we’ll query it to see what version of the MongoDB driver we have installed:
bash-3.2$ npm ls
/Users/jaygordon
└─┬ mongodb@2.2.2 
 ├── es6-promise@3.0.2
 ├─┬ mongodb-core@2.0.5
 │ ├── bson@0.5.2
 │ └─┬ require_optional@1.0.0
 │ ├── resolve-from@2.0.0
 │ └── semver@5.3.0
 └─┬ readable-stream@1.0.31
 ├── core-util-is@1.0.2
 ├── inherits@2.0.1
 ├── isarray@0.0.1
 └── string_decoder@0.10.31

So let’s prep our script once our cluster is ready, we’ll need our connection string and we’ll want to ensure our environment we’re connecting from is whitelisted. I like using this trick to validate my IP address from a command line, but you can easily go to http://icanhazip.com and get the same information:
bash-3.2$ curl icanhazip.com
192.168.0.1 (example IP) 

Great, now when you created your Atlas cluster, you selected a username and a password. Let’s get the connection string and enter these into it and then begin prepping our connection test app.
So not only does Atlas handle your infrastructure, scaling and backups… but we even provide you with a guide on how to integrate your application for your specific programming language.
This is what I just call app.js, a simple connection test:
var MongoClient = require('mongodb').MongoClient
 , format = require('util').format;
MongoClient.connect('mongodb://', function (err, db) {
 if (err) {
 throw err;
 } else {
 console.log("successfully connected to the database");
 }
 db.close();
});

We can insert our connection string along with our username and password to look something like this:
var MongoClient = require('mongodb';).MongoClient
 , format = require('util').format;
MongoClient.connect('mongodb://jay:YOURPASSWORD@nodejs-demo-shard-00-00-cbei2.mongodb.net:27017,nodejs-demo-shard-00-01-cbei2.mongodb.net:27017,nodejs-demo-shard-00-02-cbei2.mongodb.net:27017/admin?ssl=true&replicaSet=nodejs-demo-shard-0', function (err, db) {
 if (err) {
 throw err;
 } else {
 console.log("successfully connected to the database");
 }
 db.close();
});

We’ve reached a point now where we can begin testing, and it’s pretty easy to do so:
MongoDB Atlas gives you the tools to work quickly and effectively. Atlas makes it easy to build something GIANT without having to manage the systems hosting your data.