MongoDB Drivers in Nodejs
The Node.js is open source, cross-platform runtime system built on chrome’s JavaScript runtime for server side, invented by Ryan Dahl in 2009. It has been considered as one of the popular technology for server side programming as its event driven and non blocking I/O model makes it more lightweight and efficient for creating fast, scalable network applications and to build real time web APIs.

MongoDB is the rapidly growing open source, scalable and high performance NoSQL database for web applications and is the perfect fit for Node.js applications. MongoDB uses JSON like format to save documents (data) and allow to query using JavaScript. The important feature of MongoDB is its ability to store unstructured data and maintaining options to query and index the data similar to traditional database.

It is very interesting fact that both MongoDB and Node.js are built around JavaScript and JSON. Node and MongoDB in conjunction provides a powerful tool for managing, sorting , querying collection of JSON like data objects, without bothering about SQL like query concepts. Node.js has huge collection of excellent modules for different purposes. Also for integrating MongoDB features with Node.js there exist large number of modules, here I am listing some popular and easy to use MongoDB modules in Node.js.

Mongodb Native


The default official MongoDB Node.js driver allows you to use MongoDB in your application. It is an excellent easy-to-use and asynchronous node interface to MongoDB but the issue with this native driver is of having too many callbacks.

Example:

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/library', function(err, db) {
if(err) throw err;
var collection = db.collection('books').find().limit(10).toArray(function(err, docs) {
console.dir(docs);
db.close();
})
})

Mongoose


A heavy weight object modeling tool of MongoDB, designed to work with Node.js brings the concept model and schema’s to a NoSQL database. It supports option for schema based modeling of application data, type casting, application-side joins (population), validation and many other features. Using Mongoose enable us to skip some low level works and so it saves from writing our own.

Example:

var mongoose = require('mongoose');
var db = mongoose.connection;
mongoose.connect('mongodb://localhost:27017/library');
var bookSchema = new mongoose.Schema({
title: { type: String }
, author: String
, publisher: String
});
var Book = mongoose.model('Book', bookSchema);
var col = new Book({ title : 'testtitle', author : 'testauthor', publisher : 'testpublisher'});
col.save(function(err, thor) {
// your code goes here
});

Mongoskin
A lightweight MongoDB driver layer above node-mongodb-native makes it easy to work with MongoDB in Node.js. This not only allows object modeling concepts itself, but enables query chaining without using too many call back functions. Generally mongoskin provides all features of MongoDB native driver and add some more cool features like extending user defined methods.

Example:

var db = require('mongoskin').db('localhost:27017/library');
db.collection('books').find().toArray(function(err, result) {
if (err) throw err;
console.log(result);
});

Mongojs
Mongojs is another well known Node.js module for MongoDB that mimics almost all features exhibited by MongoDB shell API. It is not mandatory to use callback functions for every operations in mongojs. Mongojs is very easy to use,

Example,

var mongojs = require('mongojs');
var db = mongojs('localhost:27017/library');
var mycollection = db.collection('books');
db.mycollection.save({created:'just now'});
db.mycollection.find(function(err, docs) {
// docs is an array of all the documents in collection 'books'
});

Mongolian
Mongolian DeadBeef is a third party driver built from the light weight BSON (Binary JSON) library called Buffalo. This driver makes simple operations fast and synchronous because it doesn’t work until a query is actually made and also it uses one connection per server. This driver is trying to mimic almost like MongoDB shell API.

Example:

var Mongolian = require('mongolian')
var server = new Mongolian;
var db = server.db("library");
var bookcol = db.collection("books");
bookcol.insert({
title : 'testtitle',
author : 'testauthor',
publisher : 'testpublisher'});

Here I have listed a few a few MongoDB drivers in node js. Actually the choices for selecting MongoDB driver are overwhelming. The fact is that all drivers are built on top of the real MongoDB driver, i.e., node-mongodb-native. So deciding which one is best for you depends on the requirements you have.

Know More About This Topic from our Techies

Latest Post