node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org ยป



mongodb native middleware for mdoq.


npm install mdoq-mongodb


Requests are buffered until a connection is available. Just provide the location of a database and start making requests.

Using A Database

To specify a database, simply use() the mdoq-mongodb middleware and specify a url. Credentials, port (27015), and host (localhost) are optional.

var db = mdoq.require('mdoq-mongodb').use('mongodb://localhost/test-db');

Using A Collection

Once you are using a database, you can use() a specific collection.

var users = db.use('/users');


Mongodb methods insert(), find(), update(), and remove() are exposed as post(), get(), put(), and del().


Requests are only executed once a callback is provided. In order to find and modify an object you can chain actions.

users.get({age: {$gte: 21}).update({canDrink: true}, function(err, res) {


All mongodb query objects are supported by passing an object as an argument to get().

users.get({last_name: 'Smith'}, function(err, res) {
  console.log(res); // [...]


Calling post() will insert a document into a collection.{name: 'Jimbo Jones', faux: true}, function(err, res) {
  console.log(res); // {_id: <ObjectID>, name: 'Jimbo Jones', faux: true}


Updating a document requires a query and a modifer object. You can also supply a regular object to replace the existing document.

users.get({name: 'Bob'}).put({$inc: {views: 1}}, function(err) {
  if(!err)'update successful!');


Delete a document by simply calling del() with a query to match all the documents to be deleted.

user.get({name: 'Bob'}).del(function(err, res) {


A request can be built from a chain of actions and modifiers. Modifiers can also end the chain if passed a callback function.

users.get({faux: true}).limit(5).skip(5).sort({name: -1}).count(function(err, res) {; // [...]


All results will be returned unless a query is called with each(). This prevents buffering large sets of results, as only one result is pulled at a time.

var log = fs.createWriteStream('./log.json');

users.each(function(err, user) {


Requests called with first() will return the first object or undefined.

users.get({name: 'Joe'}).first(function(err, joe) {; // {name: 'Joe' ...}


var page = 1
  , perPage = 16
;, perPage).get(function(err, res) {; // [ 1 of users...]; // 4 (number of pages)


Sort by a given key pattern which indicates the desired order for the result.

users.sort({name: -1}).limit(16).get(function(err, sortedUsers) {; // [...]

Limit is recommended as there is a mongodb limit on the size of sorted results when an index is not used.


Include a count separately from the results by chaining count().

users.get({name: 'Bob'}).count(function(err, bobs) {'there are %n users named Bob',;'all the bob users:', bobs);

Limit / Skip

Like all other modifiers, limit() and skip() can be chained.

users.skip(10).limit(10, function(err, res) {; [...10 users at index 10...]


Add a file to a req to have it posted to the specified collections GridStore.

// reference a remote file
var avatar = users.get({_id: 'avatar.jpg'});

// upload file
avatar.file(fs.createReadStream('./avatar.jpg')).post(function(err) {
  // download file
  avatar.get(function(err, res) {
    // stream it to disk'./my-avatar.jpg'));
    // delete it
    avatar.del(function(err) { || 'deleted it!');


Return a listing of files for the collection, files) { || files);


A simple function to rename a collection.

users.rename('admins', function(err) { || 'rename success!');


Drop a collection from the db.

users.drop(function(err) { || 'dropped!');