easymongo

Simple interface for the MongoDB API

npm install easymongo
9 downloads in the last week
129 downloads in the last month

easymongo

NPM version Build Status Coverage Status Dependency Status devDependency Status

This is a small tweaks for the native MongoDB driver.

Installation

$ npm install easymongo

Examples

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(error, results) {
  // Returns a new document (array).
  console.log(results);
});

users.find({name: 'Alexey'}, {limit: 2}, function(error, results) {
  // Always return array of documents.
  console.log(results);
});

users.findById('4e4e1638c85e808431000003', function(error, results) {
  // Returns a document (object). If error occur then returns false.
  console.log(results);
});

users.count({name: 'Alexey'}, function(error, results) {
  // Amount (int). If error occur then returns zero.
  console.log(results);
});

users.remove({name: 'Alexey'}, function(error, results) {
  // Returns a result of operation (boolean). If error occur then returns false.
  console.log(results);
});

users.removeById('4e4e1638c85e808431000003', function(error, results) {
  // Returns a result of operation (boolean). If error occur then returns false.
  console.log(results);
});

API

Client class

Constructor

Arguments:

  • server (string || object) — connection url to MongoDB or object with host, port and dbname
  • options (object) — optional options for insert command

Methods

  • collection(name) — returns a new instance of the easymongo Collection class
  • open(name[, callback]) — returns the MongoDB Collection
  • close() — close the db connection

Collection class

Methods

  • 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"]

Flow control

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

Contributing

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

Author

License

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

Bitdeli Badge

npm loves you