This is a small tweaks for the native MongoDB driver.
$ npm install easymongo
var options = {
dbname: 'test'
};
var mongo = new require('easymongo')(options);
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: 1}, 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);
});
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])
- findById (oid[, callback])
- save (params[, callback])
- remove ([params][, callback])
- removeById (oid[, callback])
- count ([params][, callback])
You can use easymongo
with co for generator based flow-control. For these purposes use the co-easymongo.
The MIT License, see the included license.md
file.