mongo-wrapper
This is a lightweight wrapper around the mongodb driver for Node.js. It simplifies connecting and adds a few helper methods.
Installation
$ npm install mongo-wrapper
Usage
Configuration
var mongo = ; var config = username: 'admin_user' password: 'secret' hosts: name: 'primary-1.db.com' port: 27017 name: 'secondary-1.db.com' port: 27017 name: 'secondary-2.db.com' port: 27017 database: 'db_1' options: replicaSet: 'replicaset_1' readPreference: 'primaryPreferred' indexes: users: index: 'email' options: unique: true ; db = mongo; // add your collectionsdb;
Query
// once the db is setup and collections are added you can use any// methods found in the Mongo Driver Collection class// http://mongodb.github.io/node-mongodb-native/api-generated/collection.html dbusers; dbusers;
Additional Helper Functions
findArray()
// findArray() simply calls toArray() on the cursor returned from// Collection.find()dbusers;
findById(), updateById(), findAndModifyById()
// These functions make it simple to query by ObjectId and they// accept either and ObjectId object or a JavaScript String.dbusers;
bind()
// This allows you to bind a function on a collection so you can// easily use mongo-wrapper with libraries like asyncvar user_id = dbid'53683ca19c49151345e479ad';async;
db.id()
// a wrapper around ObjectIddbid; // creates a new ObjectIddbid'53683ca19c49151345e479ad'; // creates the ObjectIddbid'invalid id'; // returns null