Setting up MongoDB with Node.js

Tags: javascript, mongo-db, node-js

After a brief introduction post on building the Javascript application stack, the first thing I'm going to discuss in more detail is picking the database driver for accessing MongoDB. There are several options, and you have to choose which one is a fit for your use-case.

First challenges

After I've decided on the application stack,‭ ‬and played a little bit with‭ '‬Hello world‭' ‬in Node.js and Express.js‭ (‬don't miss the docs‭) ‬the first thing that was needed to set up for a real-world application was database communication,‭ ‬and the database itself.

One thing that I want for now is as less management as possible,‭ ‬so why manage my own database if I can get one in the cloud (‬especially for just playing around).‭ ‬I've visited MongoHQ‭ ‬and MongoLab‭ ‬and grabbed a free instance for my testing purposes.‭ ‬Easy and painless‭!

After I was having a test database,‭ ‬with some testing data put into it,‭ ‬I needed to connect to it from Node/Express application and send that data back to the browser as JSON output,‭ ‬to confirm things are working.

MongoDB database drivers for Node.js‭

And,‭ ‬the fun started here‭ ‬- choosing the right MongoDB driver.‭ ‬First,‭ ‬I've tried‭ the ‬native MongoDB‭ ‬driver.‭ ‬I managed to connect and pull test data immediately when‭ ‬connected,‭ ‬but couldn't figure out how to easily keep the credentials within the database/connection object to avoid repeating the authentication piece of code on each database access method.‭ ‬It's not that it isn't possible,‭ ‬it's just quite unintuitive interface for the driver which I didn't like,‭ ‬I was sure that I can have better.

Then I ran into Mongoose‭,‭ ‬a MongoDB object modeling tool with a simple and easy to use database connection interface.‭ ‬Connecting and keeping setting credentials at only one place in the code worked like charm.

But‭ ‬– there's a‭ "‬but‭"‬.‭ ‬Mongoose is great if you're building server-side Node.js application,‭ ‬as it's an object modelling tool,‭ ‬giving you schemas,‭ ‬real model objects on the server side and all the stuff you're used to in a server-side application.‭ ‬On the other hand,‭ ‬I'm building a stack for client-side Backbone application,‭ ‬and I'm interested in setting up only a thin server-side API layer just working with JSON data,‭ ‬passing it between Backbone and MongoDB.‭ ‬So,‭ ‬Mongoose didn't seem like the right fit and had to drop out.

The next thing I've ran into (through this article) is Mongoskin‭,‭ ‬and while thinking that this might be the right thing -‭ ‬wrapping native MongoDB driver into a more friendly interface -‭ ‬I also discovered MongoJS‭ ‬which is a similar wrapper,‭ ‬but emulating the command-line MongoDB API as much as possible.

That seemed like a good thing,‭ ‬meaning that you are learning only a single interface on how to communicate with the database,‭ ‬and use it from within your application and command-line database client.‭ ‬So,‭ ‬I decided to set that thing up,‭ ‬and it worked out-of-the-box with a friendly and more intuitive interface than mongodb-native driver.‭ ‬I'm keeping MongoJS for now.

Note that I haven't been talking about the performance of these drivers.‭ ‬You can find different comments by Googling,‭ ‬but,‭ ‬it's out-of-scope at the moment.

All of these may be useful info to start working with MongoDB in Node, but keep in mind that your application might have different needs, and that a different choice may be better for you.

Different data, different data modeling, different data access models. Explore!

Story comments:

blog comments powered by Disqus