NodeJS + MongoDB = easy peachy

Okay, so today i found a great service online called MongoLab, MongoLabs is a DBaaS (Database-as-a-service) which allows me to create and host a MongoDB database in the cloud with just a few clicks.

I’ve wanted to give MongoDB a go for a few of my pet projects, but managing and hosting my MongoDB database is a task I’d rather leave to the professionals, since setting up backup rutines and and that technical stuff is not my cup of tea :-)
What i think so far

One of the great things about MongoLabs is that they give me a graphical interface for viewing my collections and my data. I’m no big fan of the command line so this is a big thing for me.

Another thing i liked is how easy it was to get started. I literally took my only 5 minutes to create my account, create a database, setup a database user, and connect my app to the database.

Also it’s great that i can choose which cloud provider i want to host my MongoDB instance. This means that i can choose a provider that has a datacenter near my location, and this also allows me to get extremelly low latency since my apps are probably hosting on one of the cloud provides that they support anyways.
When writing this MongoLab had support for: AWS, Google Cloud Platform, Joyent, Rackspace and Windows Azure.

I don’t have much negative to say yet, since i’ve only just started using the service, but from what i’ve seen so far, i really like it.
Getting started

Once you’ve created your account, you create your database by giving it a name, choose a cloud provider, and choose your price tier.

Once that’s done you end up with a connection string that you can paste into you app.

He’s a small sample of what it could look like in a NodeJS app using ExpresJS and Mongoose:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect(
'mongodb://<username>:<password>@ds1337.mongolab.com:1337/database'
);

/* Create Schema */
var schemaActivity = new Schema({
title: String,
description: String
});

/* Create model */
var Activity = mongoose.model('Activity', schemaActivity);

var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

app.get('/', function(req, res) {
// Create new activity from model
var newActivity = new Activity({
title: 'Activity title',
description: 'Totally awesome activity'
});
// Save it to my MongoDB at MongoLabs
newActivity.save(function(err) {
if (err) throw Error(err);
});
res.render('index', { title: 'Express' });
});
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});

Comments: