easymongo

Simple interface for the MongoDB API

easymongo

This is a small tweaks for the native MongoDB driver.

$ npm install easymongo
var easymongo = require('easymongo');
 
var mongo = new easymongo({dbname: 'test'});
var users = mongo.collection('users');
 
var data = {name: 'Alexey', surname: 'Simonenko', url: 'http://simonenko.su'};
users.save(data, function(errorresults) {
  // Returns a new document (array). 
  console.log(results);
});
 
users.find({name: 'Alexey'}, {limit: 2}, function(errorresults) {
  // Always return array of documents. 
  console.log(results);
});
 
users.findById('4e4e1638c85e808431000003', function(errorresults) {
  // Returns a document (object). If error occur then returns false. 
  console.log(results);
});
 
users.count({name: 'Alexey'}, function(errorresults) {
  // Amount (int). If error occur then returns zero. 
  console.log(results);
});
 
users.remove({name: 'Alexey'}, function(errorresults) {
  // Returns a result of operation (boolean). If error occur then returns false. 
  console.log(results);
});
 
users.removeById('4e4e1638c85e808431000003', function(errorresults) {
  // Returns a result of operation (boolean). If error occur then returns false. 
  console.log(results);
});

Arguments:

  • server (string || object) — connection url to MongoDB or object with host, port and dbname
  • options (object) — optional options for insert command
  • collection(name) — returns a new instance of the easymongo Collection class
  • open(name[, callback]) — returns the MongoDB Collection
  • close() — close the db connection
  • find([params][, options][, callback])
  • findOne([params][, options][, callback])
  • findById(oid[, fields][, callback])
  • save(data[, callback])
  • update(params, data[, callback])
  • remove([params][, callback])
  • removeById(oid[, callback])
  • count([params][, callback])

Possible find options:

  • limit — to specify the maximum number of documents (more info)
  • skip — to control where MongoDB begins returning results (more info)
  • sort — controls the order that the query returns matching documents (more info)
  • fields — specify fields array to limit fields in returned documents, e.g. ["name", "url"]

You can use easymongo with co for generator based flow-control. For these purposes use the co-easymongo.

DO NOT directly modify the lib files. These files are automatically built from CoffeeScript sources located under the src directory.

To do build run:

$ npm run build

The MIT License, see the included license.md file.