Fetching contributors…
Cannot retrieve contributors at this time
159 lines (116 sloc) 5.31 KB


This is a node.js driver for MongoDB. It's a port (or close to a port) of the libary for ruby at

A simple example of inserting a document.

var client = new Db('integration_tests_20', new Server("", 27017, {})); {

client.createCollection(function® { client.collection(function(collection) { for(var i = 1; i < 1000; i++) { collection.insert(new OrderedHash().add('c', i), function® {}); }

collection.insert(new OrderedHash().add('a', 2), function® { collection.insert(new OrderedHash().add('a', 3), function® { collection.count(function(count) { test.assertEquals(1001, count); // Locate all the entries using find collection.find(function(cursor) { cursor.toArray(function(results) { test.assertEquals(1001, results.length); test.assertTrue(results != null);

// Let's close the db finished_test({test_insert:'ok'}); }); }, new OrderedHash()); }); }); }); }, 'test_insert'); }, 'test_insert');


GitHub information

The source code is available at You can either clone the repository or download a tarball of the latest release.

Once you have the source you can test the driver by running

$ make test_all

in the main directory. You will need to have a mongo instance running on localhost for the integration tests to pass.


For examples look in the examples/ directory. You can execute the examples using node.

$ cd examples $ node queries.js


The GridStore class allows for storage of binary files in mongoDB using the mongoDB defined files and chunks collection definition.

See the gridfs.js file under examples/ for how to use it or view the ingration tests marked with test_gs_…


The current version does not support connection pooling, but it will be implemented soon.

Primary Key Factories

Defining your own primary key factory allows you to generate your own series of id's (this could f.ex be to use something like ISBN numbers). The generated the id needs to be a 12 byte long “string”.

Simple example below

// Custom factory (need to provide a 12 byte array); CustomPKFactory = function() {} CustomPKFactory.prototype = new Object(); CustomPKFactory.createPk = function() { return new ObjectID(“aaaaaaaaaaaa”); }

var p_client = new Db('integration_tests_20', new Server(“”, 27017, {}), {'pk':CustomPKFactory}); { p_client.dropDatabase(function(done) { p_client.createCollection(function(collection) { collection.insert({'a':1}, function(doc) { collection.find(function(cursor) { cursor.toArray(function(items) { test.assertEquals(1, items.length);

finished_test({test_custom_primary_key_generator:'ok'}); p_client.close(); }); }, {'_id':new ObjectID(“aaaaaaaaaaaa”)}); }); }, 'test_custom_key'); }); });

Strict mode

Each database has an optional strict mode. If it is set then asking for a collection that does not exist will return an Error object in the callback. Similarly if you attempt to create a collection that already exists. Strict is provided for convenience.

var error_client = new Db('integration_tests_', new Server(“”, 27017, {auto_reconnect: false}), {strict:true}); test.assertEquals(true, error_client.strict); { error_client.collection(function(collection) { test.assertTrue(collection instanceof Error); test.assertEquals(“Collection does-not-exist does not exist. Currently in strict mode.”, collection.message); }, 'does-not-exist');

error_client.createCollection(function(collection) { error_client.collection(function(collection) { test.assertTrue(collection instanceof Collection); // Let's close the db finished_test({test_strict_access_collection:'ok'}); error_client.close(); }, 'test_strict_access_collection'); }, 'test_strict_access_collection'); });


The cursors allow for the application to pull data lazily from mongoDB. The query is deferred until you request the first object using either .each/.nextObject/.toArray.


Work in Progress

Release Notes



10gen: Google: Jonas Raoni Soares Silva


Copyright 2009 - 2010 Christian Amor Kvalheim.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  See the License for the specific language governing permissions and
  limitations under the License.