Monk: Lightweight, quick and usable

Monk is a somewhat popular ORM for MongoDB that provides a simple interface to interact with our Mongo database. Unlike popular choices such as Mongoose, Monk eschews most model and schema paradigms to stay more true to non-relational databases.

How it works

Monk thrives on its simplicity. It makes it super easy to get up and running with a mongo database, as Monk sets up a thin layer over Mongo commands, granting you ease of access to the database without a complex directory or schema structure. All apps using Monk start with the inherent require statements as copied below, but it also lets you link database collections to variable names:

// Assign database require to use monk
var db = require('monk')('localhost/mydb');  
// Set dogs collection to a variable called dogs
var dogs = db.get('dogs')  

At this point, let's pretend our database looks something like this. We ran three inserts on it, so it has three items:

{ "_id" : ObjectId("544183de6f44e83c17feafdc"), "name" : "Lucky", "breed" : "Beagle" }
{ "_id" : ObjectId("544183f66f44e83c17feafdd"), "name" : "Sammy", "breed" : "Golden Retriever" }
{ "_id" : ObjectId("544184116f44e83c17feafde"), "name" : "Oleg", "breed" : "American Eskimo" }

To manipulate this database now, we only have to reference the dogs variable we mentioned, and we have a number of methods in Monk that closely mirror those of Mongo:

// Insert files into the database
dogs.insert({ name: 'Fred', breed: 'Golden Retriever' }, function (err, doc) {  
  if (err) throw err;

// Find all dogs with a given characteristic
dogs.find({ breed: 'Golden Retriever'}, function(err, docs){  
  if (err) throw err;
  // docs will contain all dogs that are Golden Retrievers
  // pass docs to somewhere else

// Find one dog with a given characteristic
dogs.findOne({ breed: 'American Eskimo'}, function(err, doc){  
  if (err) throw err;
  // doc will contain one dog that is an American Eskimo -- useful for finding a specific user

// Or use promise-based callbacks
dogs.findOne({ breed: 'American Eskimo' }).on('success', function(err, doc){  
  if (err) throw err;
  //your super awesome code goes here

// Maybe you want to update something. Oleg's name is changed to Clifford
dogs.update( { name: 'Oleg' }, { name: 'Clifford' }, function(err){  
  if (err) throw err;
  console.log("Oleg's name changed to Clifford");

// Wow, that was easy!

As you can see, Monk reads a lot like basic MongoDB command line commands. An insert in mongo CLI would just be db.dogs.insert({ name: 'Fred', breed: 'Golden Retriever' }); Straightforward mongo!

When to use Monk

Monk is great when you want a database but don't want to think too hard about how you're going to implement it. You really can just throw things at your database without much structure. This is perfect if you want to rapidly iterate, but not so great for a production-grade app. When pushing to production, the models and schemas defined in ORMs like Mongoose can be essential to preserve some structure in a non-relational database.

That said, Monk is fun and easy to get started with, so check it out! If you have any questions, just post in the comments below!

comments powered by Disqus