mongo-select: A module to simplify mongoDB projections

Today I started to work on adding field selection capabilities to some of the endpoints used by the Auth0 app. Part of that involves updating the mongodb queries to limit the fields returned by them.

As this work will probably span a set of queries, I decided to create a module to simplify the creation of the projection objects used for the queries. That’s how mongo-select came to be:

As the mongodb documentation clearly explains, you can either

  • Determine a set of fields to include by specifying their name and a related truthy value { name: true }.
  • Determine a set of fields to exclude by specifying their name and a related falsy value { name: false }.

mongo-select allows you to easily specify fields to include or exclude, both on a permanent and one time basis.

Most of the rest of this post comes from the README.md at the time of writing.

Installing

To install the latest version simply run:

npm install mongo-select

Basic example

The following sample code shows how you can work with the native mongodb NodeJs driver to exclude the user field:

var select = require('mongo-select').select();
var mongodb = require('mongodb');

var MongoClient = mongodb.MongoClient;

MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
  if(err) throw err;

  var users = db.collection('users');
  scripts = users.find({}, select.include(['name', 'email']), 
    function(err, result){
      // code here, access to only result[i]._id, result[i].name and result[i].email
    });
});

Including fields

To include fields use the following:

var select = require('mongo-select').select();

var projection = select.include(['name', 'email', 'children.name']);

console.log(projection); // { 'name': false, 'email': false, 'children.name': false };

Excluding fields, no _id and chaining

You can also exclude fields using the exclude method and the _id field using noId(). You can chain exclusions/inclusions and noId(). One thing to consider is that in order to provide a fluent interface the chaining methods begin with _. Otherwise this might affect documents with fields named exclude, include, noId.

var select = require('mongo-select').select();

var projection = select.exclude(['name'])._exclude(['email', 'children.name'])._noId();

console.log(projection); // { '_id': false, 'name': false, 'email': false, 'children.name': false };

Permanent exclusion/inclusion

Sometimes it is important to always exclude or include a set of fields. That means that if they were permanently excluded and then specifically included they won’t make it into the projection and viceversa:

var select = require('mongo-select').select();

select.exclude(['name', 'email', 'children.name'])._always();

var exclusion = select.exclude(['address']);

console.log(exclusion); // { 'name': false, 'email': false, 'children.name': false };

var inclusion = select.include(['name', 'email']);

console.log(inclusion); // { };

To clear permanent registrations simply:

select.clear();

Wrapping up

I’m totally up for contributions via PRs or issues, so if you have some feedback about the package or improvement ideas feel free to jump in.