README
mniam
Yet another mongodb native driver facade. Takes care of:
- mongo URI parsing
- opening and closing DB connections
- opening collections
Install by running
npm install mniam
API
database(url, [options])
Connect to database mniam-test
and create friends
collection with index on name
field.
var db = database('mongodb://localhost/mniam-test'),
friends = db.collection({
name: 'friends',
indexes: [[{ name: 1 }]]
});
Mniam is using MongoClient to establish the connection: full mongo database URLs are supported. The database function also takes a hash of options divided into db/server/replset/mongos allowing you to tweak options not directly supported by the unified url string format.
var db = database('mongodb://localhost/mniam-test', {
db: {
w: -1
},
server: {
ssl: true
}
})
collection.save
Add a new documents:
friends.save({
name: 'Alice',
age: 14,
}, function(err, item) {
console.log('Item id:', item._id);
});
collection.findAndModify
Update a document:
friends.findAndModify(item._id, {
$set: { age: 15 }
}, function(err, item) {
console.log('Alice is now:', item.age);
})
collection.remove
Remove a document:
friends.remove({ name: 'Alice' }, function(err) {
// last collection closed closes DB connection
friends.close();
});
Iteration
Use query
, fields
and options
to create and configure cursor.
Iterate over the results of the query using toArray
, eachSeries
, eachLimit
methods.
toArray
- converts query results into array
friends.query({ age: { $gt: 21 } }).toArray(function(err, arr) {
console.log('My friends over 21 years old', arr);
});
eachSeries
- callsonItem
sequentially for all results
friends.query().fields({ name: 1 }).eachSeries(function onItem(item, fn) {
console.log('Name:', item.name);
fn();
}, function done(err) {
console.log('All friends listed.');
});
eachLimit
- iterates over all results callingonItem
in parallel, but no more thanlimit
at a time
friends.query().options({ sort: { age: 1 } }).eachLimit(4, function onItem(item, fn) {
console.log('Friend', item);
fn();
}, function done(err) {
console.log('All friends listed.');
});
Aggregation
Mniam collections provides flex API for aggregation pipeline:
friends
.aggregate() // start pipeline
.project({ author: 1, tags: 1 })
.unwind('$tags')
.group({
_id : { tags : '$tags' },
authors : { $addToSet : '$author' },
count: { $sum: 1 }
})
.sort({ count: -1 })
.toArray(function(err, results) {
console.log(results);
});
In addition to toArray
you can use eachSeries
and eachLimit
to iterate over aggregation results.
Each aggregation stage ($project
, $unwind
, $sort
, etc.) has a corresponding function with the same
name (without $
). You can also pass a traditional array of stages to .pipeline(stages)
method, and set
options with .options({})
method.
Other collection methods supported
collection.geonear
collection.findOne
collection.update
License
MIT