Getting Mongo to Work (the hard way): node.js day three-five
Table of Contents:
- Getting Mongo to Work (the hard way): node.js day three-five
- From Procedural code to homebrew ORM, with a False Start.
- Callback to move forward.
- Fall Back To Move Foward
- A return to abstraction
- Mixing In Goodness
I am an ORM junkie and have been stalking the Mongoose people for the latest version of their mongo orm for node. However as I've not been able to get the latest release from them to work (at the time of writing, their docs are still for v 0.0.3) I am using the default Mongo library and the blog article as a guide for creating a collection provider module.
But sometimes what you set out to do is not (directly) what you accomplish. Instead of handily adapting an existing example, I did a few cycles with the core Mongo node libraries and got to know callbacks and mixins a lot better.
This article is a bit verbose and lengthy; if you just want "code you can use" skip to the last page. However I thought for those who, like me, are converting from another SS language to JavaScript in order to take advantage of Node, it would be useful to document my false starts and stumbling points on the way to getting something to work in node. In the "Mythbusters" spirit of "Do it again til you get it right", it took me a while to get this superficially solved task to perform -- hopefully others will get something out of the extended narrative.
Earlier I got the rough example of using the mongodb libraries working:
GLOBAL.DEBUG = true;
sys = require("sys");
//test = require("mjsunit");
var mongo = require('mongodb');
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : mongo.Connection.DEFAULT_PORT;
var LINE_SIZE = 120;
sys.puts("Connecting to " + host + ":" + port);
var db = new mongo.Db('node-mongo-blog', new mongo.Server(host, port, {}), {});
db.open(function(err, db) {
db.dropDatabase(function(err, result) {
sys.puts("===================================================================================");
sys.puts(">> Adding Authors");
db.collection('authors', function(err, collection) {
collection.createIndex(["meta", ['_id', 1], ['name', 1], ['age', 1]], function(err, indexName) {
sys.puts("===================================================================================");
var authors = {};
// Insert authors
collection.insert([{'name':'William Shakespeare', 'email':'william@shakespeare.com', 'age':587},
{'name':'Jorge Luis Borges', 'email':'jorge@borges.com', 'age':123}], function(err, docs) {
docs.forEach(function(doc) {
sys.puts(sys.inspect(doc));
authors[doc.name] = doc;
});
});
sys.puts("===================================================================================");
sys.puts(">> Authors ordered by age ascending");
sys.puts("===================================================================================");
collection.find({}, {'sort':[['age', 1]]}, function(err, cursor) {
cursor.each(function(err, author) {
if(author != null) {
sys.puts("[" + author.name + "]:[" + author.email + "]:[" + author.age + "]");
} else {
sys.puts("===================================================================================");
sys.puts(">> Adding users");
sys.puts("===================================================================================");
db.collection('users', function(err, userCollection) {
var users = {};
userCollection.insert([{'login':'jdoe', 'name':'John Doe', 'email':'john@doe.com'},
{'login':'lsmith', 'name':'Lucy Smith', 'email':'lucy@smith.com'}], function(err, docs) {
docs.forEach(function(doc) {
sys.puts(sys.inspect(doc));
users[doc.login] = doc;
});
});
sys.puts("===================================================================================");
sys.puts(">> Users ordered by login ascending");
sys.puts("===================================================================================");
userCollection.find({}, {'sort':[['login', 1]]}, function(err, cursor) {
cursor.each(function(err, user) {
if(user != null) {
sys.puts("[" + user.login + "]:[" + user.name + "]:[" + user.email + "]");
} else {
sys.puts("===================================================================================");
sys.puts(">> Adding articles");
sys.puts("===================================================================================");
db.collection('articles', function(err, articlesCollection) {
articlesCollection.insert([
{ 'title':'Caminando por Buenos Aires',
'body':'Las callecitas de Buenos Aires tienen ese no se que...',
'author_id':authors['Jorge Luis Borges']._id},
{ 'title':'I must have seen thy face before',
'body':'Thine eyes call me in a new way',
'author_id':authors['William Shakespeare']._id,
'comments':[{'user_id':users['jdoe']._id, 'body':"great article!"}]
}
], function(err, docs) {
docs.forEach(function(doc) {
sys.puts(sys.inspect(doc));
});
})
sys.puts("===================================================================================");
sys.puts(">> Articles ordered by title ascending");
sys.puts("===================================================================================");
articlesCollection.find({}, {'sort':[['title', 1]]}, function(err, cursor) {
cursor.each(function(err, article) {
if(article != null) {
sys.puts("[" + article.title + "]:[" + article.body + "]:[" + article.author_id.toHexString() + "]");
sys.puts(">> Closing connection");
db.close();
}
});
});
});
}
});
});
});
}
});
});
});
});
});
});

Watch npm for module
I will put this into NPM after I've ran it around the barn a few times. Is anyone else using this?
We're not there yet
The next morning I decided to stop trusting my console messages and check mongo. Surprise! no data.
This is my fault - I was not using callbacks to delay certain actions. First proof is that when I put the entire body (like 3+ of my insert test script into a callback that fires when the database is created, then at least, the database is now being created in MongoDB. Data is not inserting ... but it's a start.
Post new comment