3 Easy Steps to Get The Most from MongoDB and Node.JS

07 Aug 2013

steak and wineMongoDB and Node.JS go together like good red wine and steak - that is to say, extremely well. This happy scenario owes to the fact that MongoDB and Node.JS are both built around JavaScript and JSON. MongoDB handles JavaScript natively - there is no impedence mismatch trying to shoe-horn objects into a relational model. Simply persist and query JSON.

However, not everything is consistently rosy when using Node.JS and MongoDB together. There are caveats to be aware of. As with any database, your experience using MongoDB is likely only going to be as good as your chosen driver. No matter how wonderful your database is, if you are using a subpar bug-ridden driver with it, your adventures with it will not be pleasant.

1. Use A Good MongoDB Driver

MongoDB logo

 

 

Based on our experience with customers who have high-volume applications running on MongoDB, we recommend you to use either Mongoose or MongoSkin as your MongoDB driver.

Mongoose

Mongoose is the heavyweight ORM for Node.js. It supports schemas, validation, application-side joins (population), plugins and many other features. For a larger application, you probably will want validation. Mongoose already provides this and so it saves you writing your own.

On the other hand, if you only want to run a few simple queries, or you are allergic to ORMs, Mongoose may be overkill for your needs.

MongoSkin

MongoSkin is a lightweight MongoDB driver for Node.JS. It is somewhat similar to Python's pymongo driver in scope. It doesn't provide ORM features itself, but enables you to chain queries without too many callbacks. Far nicer than using the very low level node-mongodb-native driver.

2. Have A Sane Data Model

data model graphic

MongoDB can support complicated data models with excellent performance, but you definitely want to think out your schema and application requirements in advance.

In particular, pay attention to size/growth:

  • If embedding, will you exceed maximum document size (currently 16MB)
  • How fast will your collections grow?
  • Will all your data fit in main memory?
  • How many indexes do you need?
and access patterns:
  • What are the most common application queries and how can the results most efficiently be generated?
  • Are you read-heavy or write-heavy?
  • Do you need atomicity guarantees?

3. Use A Good MongoDB Host

While MongoDB is easy to get up and running on your local machine, when it comes to managing a production server (with replica sets, backups, etc) you really need to know what you're doing.

For this reason, we recommend you outsource MongoDB hosting to a provider like MongoLab (disclaimer: we're MongoLab partners and use them for our own stuff), MongoHQ or ObjectRocket rather than doing it yourself. Certainly at least until you have enough traffic to justiy investing in your own infrastructure.

Need More Help...?

If you need more help with your MongoDB setup or Node.JS, we're happy to help. We offer Node.JS/MongoDB architectural sanity checking, troubleshooting, firefighting and implementation. Email us at  or use the contact form at frozenridge.co.