mongodb-querybuilder
mongodb-querybuilder
is a javascript helper that provides some convenience methods to easily build MongoDB
Aggregation Framework pipelines. It is based on the concept of Fluent APIs and automatically handles a lot of the necessary pipeline stages that are just a means to an end (like $unwind
ing, $project
ing nested fields to the top level, etc).
Installation
npm install
Testing
npm test
And then open http://localhost:8080/__zuul in a browser.
Dist
Of course the preferred way to use -querybuilder is npm+browserify, but sometimes being able to just drop a script tag into a codepen is nedded.
- Create a GitHub Access Token
- Export your access token as
GITHUB_TOKEN={{YOUR TOKEN}}
npm run-script dist
Todo
- Setup travis for saucelabs. see zuul docs
- Write lots more tests
Example
The query builder turns that:
builder ;
into that:
Full example (see also index.html):
QueryBuilder Test
API
QueryBuilder(options)
Constructor to create a new QueryBuilder. Takes an options
hash. The options are:
type | values |
---|---|
scope | hostname/port of mongoscope, passed on to mongoscope-client |
seed | hostname/port of MongoDB database, passed on to mongoscope-client |
namespace | namespace to query against, in database.collection format |
samples | number of samples to create schema from |
Example
var builder = scope: "http://localhost:29017" seed: "mongodb://localhost:27017" namespace: "foo.bar" samples: 500;
match(field, value)
Specify a filter to be matched when querying for documents. This turns into the $match
aggregation stage. The value
parameter is interpreted differently depending on the type of field
:
type | values |
---|---|
boolean | value is expected to be a single value, either true or false |
number | value is expected to be an array of 2 values [min , max ]. If either of the values is null or undefined , the range is considered open on that side. |
date | Same as number. Both Date() objects and strings can be provided. |
category | value is expected to be an array of possible values. If only one value is provided, the match is an equality match, if two or more values are provided, the stage is using $in to find the matches. |
If a .match()
call on the same field is repeated, the value of that field is overwritten. Specifying a value of null
or undefined
removes the filter on this field.
If multiple .match()
filters on different fields are specified, the resulting documents have to match all filters.
Example
builder // match users with these last names // match users with age between 18 and 36 (inclusive) // match users created between these dates
group(name, field)
Group documents by their value of field
and projects this value to a new field named name
. MongoDB combines grouping and aggregating (or "rolling up" values) into a single $group
stage. Therefore, a call to .group()
is usually followed by one or more calls to .agg()
.
Example
builder
This example groups all documents by their user.address.zip_code
field, and renames the field to zip
in the process. For each group, the total number of documents is calculated as count
and the average age of the users is calculated as average_age
. The result could look like this:
Multi-Field Groups
You can also group on multiple fields at once. The syntax is the same as above, but instead of a single field
value, specify an array of values. The resulting groups cover all combinations of the compound group key.
Due to a limitation of the aggregation framework around dot-notation keys (they can only appear at the top level), all dots are replaced with underscores in the resulting compound group key (e.g. user_address_zip_code
instead of user.address.zip_code
).
Example
builder
Nested Groups
Example
builder