congo is a thin wrapper for node-mongodb-native that makes connections and collections a little easier.
npm install congo
npm install https://github.com/martinrue/congo/tarball/master
The first thing you must do is call configure
to tell congo where the database is and various other options about how the database connection should be made.
var database = require('congo');
database.configure('localhost', 'mydb');
var database = require('congo');
var config = {
host: 'localhost',
name: 'mydb',
port: 27017,
reconnect: true,
pool: 10
};
database.configure(config);
After the call to configure
, call get
to retrieve a database object.
database.get(function(err, db) {
// db is a connected database object
});
All non-system collections are attached directly onto the database object for you, so querying a collection is as simple as:
database.get(function(err, db) {
db.users.findOne({}, function(err, user) {
});
});
For convenience, congo also attaches a new findAll
function to each collection object. This allows you to avoid having to deal with the cursor if you simply want to retrieve all results from a multi-result query:
database.get(function(err, db) {
db.users.findAll({}, function(err, users) {
// users is an array of all users from the database
});
});
Because congo queries all existing collections and attaches them to the database object, any new collections you intend to create won't have a corresponding db.[collection]
. For this reason, you can supply an array of collections in the config that are always mapped onto the database object:
var database = require('congo');
var config = {
host: 'localhost',
name: 'mydb',
port: 27017,
reconnect: true,
pool: 10,
collections: ['users', 'products', 'orders']
};
database.configure(config);
database.get(function(err, db) {
// db.users, db.products and db.orders will all be available,
// irrespective of whether they existed in the database or not
});
One pooled database connection is created internally and congo reuses it. This means you can call get
anywhere and not worry about creating unnecessary additional connections.
If reconnect: true
is set on the configuration object, the mongo driver will reconnect automatically and this also queues commands and replays them once connections are reestablished.
If reconnect: false
is set (the default), congo itself reconnects closed connections on each get
call and does not queue any failed commands.
Database events such as 'close'
and 'error'
are propogated. You can listen to these by passing a callback to the on
function:
database.on('close', function() {
// database connection was closed
});
database.on('error', function() {
// error occurred on the database
});
The ObjectID
et al. functions that you would normally be able to access from require('mongo').[func]
have all been extended onto congo and can be used in exactly the same way:
var database = require('congo');
database.get(function(err, db) {
db.users.findOne({ _id: database.ObjectID('...') }, function(err, user) {
});
});