node package manager


Fork of node-mongodb-wrapper


A wrapper for node-mongodb-native as close as possible to the native javascript driver. Why learn two interfaces?

Yes, we know other people are doing the same thing. This one has been easier to use.

  1. Minimal interface closely matching the command-line driver:
  2. Lazy open/close of connections
  3. Most features of node-mongodb-native
  npm install mongodb-wrapper
  1. You have to tell the db object which collections you're about to use (Harmony Proxies, I need you!)
  2. You have to provide callbacks on "actionable" calls (toArray, count, but not find)
  3. Otherwise, just like the native javascript driver
	var mongo = require('mongodb-wrapper')
	var db = mongo.db('localhost', 27017, 'test')
	db.collection('posts'){title: "A new post", body: "Here is some text"}, function(err, post) {
		db.posts.findOne({_id: doc._id}, function(err, post) {
			db.posts.find().limit(1).toArray(function(err, posts) {
				// posts[0].title == "A new post"

For more examples, please look at the test suite

Remember the guiding principle: the syntax exactly matches the command-line driver, except you pass a call back to any function that hits the database.

mongo.db(host, port, dbname, [prefix], [username], [password]) - returns an unopened database object.

  • If prefix is specified all collections will use the prefix in mongo, but you refer to them without the prefix in node.
  • If username and password are specified, it will attempt to authenticate.

db.collection(name) - Returns a Collection object. Also creates db[name] so you can do this:


Replica sets are also supported with an alternate function signature:

var hostsArray = [
  // opts is a hash of mongodb-native server opts:
  // also optional
  {host: "host1", port: 27017, opts: {}},
  {host: "host1", port: 27018, opts: {}},
// other replica set opts, such as read_secondary, are passed in here
// these opts are also passed to each mongodb-native server object, so you can have defaults for your servers
var opts = {rs_name: "myReplicaSet"}
mongo.db(hostsArray, opts, dbname, [prefix], [username], [password])

db.auth(username, password, cb) - You can pass username and password into mongo.db instead of calling this manually

db.addUser(username, password, cb)

db.removeUser(username, password, cb)


db.lastError(cb) - cb(err, lastError)

db.eval(code, [parameters], cb)

db.createCollection(name, options, cb) - allows you to create a collection by hand if you want to specify the options

collection.ensureIndex(index, options, cb)


collection.renameCollection(newName, dropTarget, cb)

collection.insert(doc(s), cb)

collection.remove(selector, cb)

collection.drop(cb), cb)

collection.update(selector, updates, [upsert], [multi], cb)


collection.findAndModify(options, cb)

collection.find(selector, fields) - Returns a Cursor

collection.findOne(selector, fields, cb), cb)

collection.mapReduce(map, reduce, options, cb) - map and reduce can be functions, it will toString them for you.

collection.distinct(key, [query], cb)







mongo.ObjectID - you need to wrap any string ids in this class to match on _id