underscore.aggregate

Mongodb-like Aggregation Pipeline for underscorejs


Keywords
underscore, underscorejs, aggregate, aggregation, pipeline
License
MIT
Install
bower install underscore.aggregate

Documentation

Underscore.aggregate Build Status

Aggregation features for underscorejs.

Basic example:

_([
    { level: 'warn',  date: '1999-12-31 23:59' },
    { level: 'debug', date: '2000-01-01 00:00' },
    { level: 'warn',  date: '2000-01-01 02:00' },
    { level: 'error', date: '2000-01-01 03:00' },
    { level: 'error', date: '2000-01-01 03:30' }
])
.$map({ // transform items for the next stage
    level: '$level', 
    date: { $parse: '$date' } // parse string to momentjs date object
})
.$where({  // filter items that match following conditions
    level: { $in: ['warn', 'error'] },
    date: {
        $gte: moment('2000-01-01 00:00'), // greater than or equal to ...
        $lt: moment('2000-01-02 00:00')   // less than ...
    }
})
.$group({ // group items by level attribute
    _id: '$level',
    count: { $sum: 1 },
    start: { $min: '$date' }
})
.$map({ // cleanup/format items for display
    level: '$_id',
    count: 1,
    startedAt: { $format: ['$start', 'LT']}
}).first(10).value();
// => [{
//     level: "warn",
//     count: 1,
//     startedAt: "2:00 AM"
// }, 
// {
//     level: "error",
//     count: 2,
//     startedAt: "3:00 AM"
// }]

Note:

  • works on nested objects too.
  • underscore.aggregate functions return wrapped objects on which you can apply any of underscorejs functions.

Installation and usage

  1. Choose your preferred method of installation:
  1. Include underscore, moment and underscore.aggregate in your application:
<script src="components/moment/moment.js"></script>
<script src="components/underscore/underscore.js"></script>
<script src="components/underscore.aggregate/underscore.aggregate.min.js"></script> 

Reference

See Quick Reference for a more compact overview.

Syntax:

_(collection)
   .<stage1>(<options1>)
   .<stage2>(<options2>)
   ...
   .value();

Supported functions are:

Note: underscore.aggregate functions start with '$' and return a wrapped object that can be used for further chaining.

$group (v1.0.0+)

Usage:

_(collection).$group({ 
    _id: <expression>, 
    <field1>: { <accumulator1> : <expression1> }, 
    ... 
})...

See below for all supported accumulators.

Example:

_([
    { name: 'Maggie', gender: 'female', age: 2 },
    { name: 'Lisa',   gender: 'female', age: 8 },
    { name: 'Bart',   gender: 'male' ,  age: 10 },
    { name: 'Homer',  gender: 'male' ,  age: 38 },
    { name: 'Marge',  gender: 'female', age: 40 }
]).$group({
    _id: '$gender', // group by gender
    count: { $sum: 1 },
    avgAge : { $avg: '$age' },
    maxAge : { $max: '$age' },
    minAge : { $min: '$age' },
    names :  { $addToSet: '$name'}
});

// =>[
//    { "_id": "female", "count": 3, "avgAge": 16.67, "maxAge": 40, "minAge": 2 , "names": [ "Maggie", "Lisa", "Marge" ] },
//    { "_id": "male",   "count": 2, "avgAge": 24,    "maxAge": 38, "minAge": 10, "names": [ "Bart", "Homer" ] }
// ]  

$match (v1.0.0+)

alias: $filter, $where

Filters the items that match the specified condition(s).

Usage:

_(collection).$match({ 
    <query1>, 
    <query2>, 
    ... 
})...

Supported syntax for queries :

Syntax Version Description
<field>: <expression> 1.0.0+ Matches items for which field value equals to expression evaluation.
<field>: { <operator>: <expression> } 1.0.0+ Matches items for which field value satisfy operator's condition.
<field>: { <operator>: [ <expr1>, <expr2>, ... ] } 1.0.0+ Matches items for which field value satisfy operator's conditions
<operator>: [ <query1>, <query2>, ... ] 1.0.0+ Matches items for which operator conditions are satisfied

Note: Default operator for queries is $eq. Syntax like { <field>: <expression> } } are evaluated as { <field>: { $eq: <expression> } }

Supported Operators:

Name Version Syntax Description
$gt 1.0.0+ { field: {$gt: <expression> } } Matches values that are greater than the value specified in the query. Supports both numbers and dates.
$gte 1.0.0+ { field: {$gte: <expression> } } Matches values that are greater than or equal to the value specified in the query. Supports both numbers and dates.
$lt 1.0.0+ { field: {$lt: <expression> } } Matches values that are less than the value specified in the query. Supports both numbers and dates.
$lte 1.0.0+ { field: {$lte: <expression> } } Matches values that are less than or equal to the value specified in the query. Supports both numbers and dates.
$in 1.0.0+ { field: { $in: [<expr1>, <expr2>, ... ] } } Matches any of the values that exist in the specified array.
$nin 1.0.0+ { field: { $nin: [<expr1>, <expr2>, ... ] } } Matches values that do not exit in the specified array.
$eq 1.0.0+ { field: {$eq: <expression> } } Matches all values that are equal to the specified value.
$ne 1.0.0+ { field: {$ne: <expression> } } Matches all values that are not equal to the specified value.
$and 1.0.0+ { $and: [ { <query1> }, { <query2> } , ... ] } Matches all values that satisfy all given queries.
$or 1.0.0+ { $or: [ { <query1> }, { <query2> } , ... ] } Matches all values that satisfy any of the given queries.
$nor 1.0.0+ { $nor: [ { <query1> }, { <query2> } , ... ] } Matches all values that do not satisfy any of the given queries.
$regex 1.0.0+ { field: {$regex: <RegExp> } } Matches all values that match the given pattern.

Example:

_([
    { level: 'warn',  date: moment('1999-12-31 23:59') },
    { level: 'debug', date: moment('2000-01-01 00:00') },
    { level: 'info',  date: moment('2000-01-01 01:00') },
    { level: 'warn',  date: moment('2000-01-01 02:00') },
    { level: 'error', date: moment('2000-01-01 03:00') },
    { level: 'info',  date: moment('2000-01-01 04:00') }
]).$match({
    level: { $in: ['warn', 'error'] },
    date: { $gt: moment('2000-01-01 02:00') },
}).first();  // => { level: 'error', date: {...} }

$project (v1.0.0+)

alias: $map

Transform input item for the next stage. The specified fields can be existing fields from the input object or newly computed fields.

Usage:

_(collection).$project({ 
    <specification1>, 
    <specification2>, 
    ... 
});

Supported syntax for specifications :

Syntax Version Description
<field>: <1 or true> 1.0.0+ Specify the inclusion of a field.
<field>: <expression> 1.0.0+ Add a new field or reset the value of an existing field.

Example:

_([{
    name: 'Homer Simpson',
    gender: 'male' ,
    address : { n: 742, road: 'Evergreen Terrace', city: 'Springfield' },
    birthday: moment('1955-05-12')
}]).$project({
    name : 1,
    address: { $format: '{address.n} {address.road}, {address.city}' },
    age : { $diff: [ moment(), '$birthday', 'years' ] }
}).first();
// => {   
//    name: "Homer Simpson", 
//    address: "742 Evergreen Terrace, Springfield", 
//    age: 59 
// }

Note: See expressions for more information about options.

$objectify (v1.2.0+)

Reduce items to a single object using _key/_value pairs

Usage:

_(collection).$objectify({ _key: <expression>, _value: <expression> });

Notes:

  • Default values are { _key: '$_id', _value: '$' }.
  • _key expression must resolve to a unique value for each item.

Example:

_([
    { type: 'a', count : 1 },
    { type: 'b', count: 2 },
    { type: 'c', count: 3 }
]).$objectify({ _key: '$type', _value: '$count'}); // => { a: 1, b: 2, c: 3 }

$sort (v1.3.0+)

alias: $order, $sortBy, $orderBy

Returns items in sorted order.

Takes an object that specifies the field to sort by and the respective sort order. <sort order> can have one of the following values:

  • 1 to specify ascending order.
  • -1 to specify descending order.

Usage:

_(collection).$sort({ <field>: <sort order> });

Example:

_([
    { type: 'a', count : 3 },
    { type: 'b', count: 1 },
    { type: 'c', count: 2 }
]).$sort({ count: -1 }).value(); 
// => [
//     { type: 'a', count : 3 },
//     { type: 'c', count: 2 },
//     { type: 'b', count: 1 }
// ]

Expressions

Expressions can include :

Field paths expressions

Use $<field> to specify the field path.

Field pass expressions can be nested. Example: $foo.bar.

Literal expressions

They can be of any type. However, underscore.aggregate parses string literals that start with a dollar sign $ as a path to a field and numeric/boolean literals in expression objects as projection flags. To avoid parsing literals, use the $literal expression.

Example : foo : { $literal : <value> }

Operator expressions

They are similar to functions that take arguments. In general, these expressions take an array of arguments and have the following form: { <operator>: [ <argument1>, <argument2> ... ] }. If operator accepts a single argument, you can omit the outer array designating the argument list: { <operator>: <argument> }.

Available operators are listed bellow.

Accumulators

Note: accumulators are available only for $group stage.

Name Version Description
$sum 1.0.0+ Returns a sum for each group. Ignores non-numeric values.
$avg 1.0.0+ Returns an average for each group. Ignores non-numeric values.
$first 1.0.0+ Returns a value from the first document for each group..
$max 1.0.0+ Returns the highest expression value for each group.
$min 1.0.0+ Returns the lowest expression value for each group.
$any 1.0.0+ Returns true if any elements of a set evaluate to true; otherwise, returns false.
$addToSet 1.0.0+ Returns an array of unique expression values for each group. Order of the array elements is undefined.
$fn 1.0.0+ Compute custom value based on group items.

Example:

_([
    { name: 'Maggie', gender: 'female', age: 2 },
    { name: 'Lisa',   gender: 'female', age: 8 },
    { name: 'Bart',   gender: 'male' ,  age: 10 },
    { name: 'Homer',  gender: 'male' ,  age: 38 },
    { name: 'Marge',  gender: 'female', age: 40 }
]).$group{
    _id: '$gender',
    count: { $sum: 1 },
    avgAge : { $avg: '$age' },
    maxAge : { $max: '$age' },
    minAge : { $min: '$age' },
    names :  { $addToSet: '$name'}
}).value();

// =>[
//    { "_id": "female", "count": 3, "avgAge": 16.67, "maxAge": 40, "minAge": 2 , "names": [ "Maggie", "Lisa", "Marge" ] },
//    { "_id": "male",   "count": 2, "avgAge": 24,    "maxAge": 38, "minAge": 10, "names": [ "Bart", "Homer" ] }
// ]  
Arithmetic
Name Version Description
$add 1.0.0+ Adds numbers to return the sum. Accepts any number of argument expressions.
$divide 1.0.0+ Returns the result of dividing the first number by the second. Accepts two argument expressions.
$mod 1.0.0+ Returns the remainder of the first number divided by the second. Accepts two argument expressions.
$multiply 1.0.0+ Multiplies numbers to return the product. Accepts any number of argument expressions.
$subtract 1.0.0+ Returns the result of subtracting the second value from the first. Accepts two argument expressions.

Example : 

_([{ a: 1, b: 2 }]).$project({
    add         : { $add: ['$a', '$b', 2] },
    divide      : { $divide: ['$a', '$b'] },
    mod         : { $mod: ['$a', '$b'] },
    multiply    : { $multiply: [10, '$a', '$b'] },
    $subtract   : { $subtract: ['$a', '$b'] }
}).value();
// => [{ "add": 5, "divide": 0.5, "mod": 1, "multiply": 20, "$subtract": -1 }]
String
Name Version Description
$substr 1.0.0+ Returns a substring of a string, starting at a specified index position up to a specified length. Accepts three expressions as arguments: the first argument must resolve to a string, and the second and third arguments must resolve to integers.
$format 1.0.0+ Returns a string formated according to replacement arguments. Accepts any number of argument expressions.
$toLower 1.0.0+ Converts a string to lowercase. Accepts a single argument expression.
$toUpper 1.0.0+ Converts a string to uppercase. Accepts a single argument expression.

Example : 

_([
    { hello: 'Hello', world: 'World' }
]).$project({
    format1: { $format: ['{hello} {world}!'] },
    format2: { $format: ['{0} {1}!', '$hello', '$world'] },
}).$project({
    hello: { $substr: [ '$format1', 0, 5 ] } ,
    world: { $substr: [ '$format2', 6, 5 ] } ,
    format1: { $toLower: '$format1' },
    format2: { $toUpper: '$format2' }
}).first(); 
// => {   
//      "hello": "Hello",
//      "world": "World",
//      "format1": "hello world!",
//      "format2": "HELLO WORLD!"
//  ]
Boolean
Name Version Description
$and 1.0.0+ Returns true only when all its expressions evaluate to true. Accepts any number of argument expressions.
$or 1.0.0+ Returns true when any of its expressions evaluates to true. Accepts any number of argument expressions.
$not 1.0.0+ Returns the boolean value that is the opposite of its argument expression. Accepts a single argument expression.

Example : 

_( [
    {a: true,  b: false }
]).$project({
    a   : 1,
    b   : 1,
    and : { $and: ['$a', '$b'] },
    or  : { $or: ['$a', '$b'] },
    notA: { $not: '$a' },
    notB: { $not: '$b' }
}).$project : {
    and : { $format: ['{a} and {b} => {and}'] },
    or  : { $format: ['{a} or {b} => {or}'] },
    notA: { $format: ['not {a}  => {notA}'] },
    notB: { $format: ['not {b} => {notB}'] }
}).first();
// =>  {
//    and : "true and false => false",
//    or  : "true or false => true",
//    notA: "not true => false",
//    notB: "not false => true"
// }
Comparison

Note: all comparison operators accept two argument expressions: <operator>: [ <expr1>, <expr2> ]

Name Version Description
$eq 1.0.0+ Returns true if the values are equivalent.
$ne 1.0.0+ Returns true if the values are not equivalent.
$gt 1.0.0+ Returns true if the first value is greater than the second.
$gte 1.0.0+ Returns true if the first value is greater or equal to the second.
$lt 1.0.0+ Returns true if the first value is less than the second.
$lte 1.0.0+ Returns true if the first value is less or equal to the second.

Example : 

_([
    { a: 1, b: 2 }
]).$project({
    eq: { $eq: ['$a', '$b'] },
    ne: { $ne: ['$a', '$b'] },
    gt: { $gt: ['$a', '$b'] },
    gte: { $gte: ['$a', '$b'] },
    lt: { $lt: ['$a', '$b'] },
    lte: { $lte: ['$a', '$b'] }
}).first(); 
// => {
//    "eq": false,    
//    "ne": true,      
//    "gt": false,
//    "gte": false,   
//    "lt": true,   
//    "lte": true  
// }
Array
Name Version Description
$size 1.0.0+ Returns the number of elements in the array. Accepts a single expression as argument.

Example : 

_([
    { array: _.range(10) }
]).$project({ 
    len: { $size: '$array' }
}).first(); // => { len: 10 }
Date
Name Version Description
$dayOfMonth 1.0.0+ Returns the day of the month for a date as a number between 1 and 31. Accepts a single argument expression resolving a moment object.
$dayOfWeek 1.0.0+ Returns the day of the week (Locale Aware) as a number between 0 and 6 according to the locale. Accepts a single argument expression resolving a moment object.
$dayOfYear 1.0.0+ Returns the day of the year as a number between 1 and 366. Accepts a single argument expression resolving a moment object.
$hour 1.0.0+ Returns the hour as a number beetween 0 and 23. Accepts a single argument expression resolving a moment object.
$millisecond 1.0.0+ Returns the millisecond as a number between 0 and 999. Accepts a single argument expression resolving a moment object.
$minute 1.0.0+ Returns the minute as a number between 0 and 59. Accepts a single argument expression resolving a moment object.
$month 1.0.0+ Returns the month as a number between 0 and 11. Accepts a single argument expression resolving a moment object. Note: Months are zero indexed, so January is month 0.
$second 1.0.0+ Returns the second as a number between 0 and 59. Accepts a single argument expression resolving a moment object.
$week 1.0.0+ Returns the week (Locale aware) as a number between 1 and 53. Accepts a single argument expression resolving a moment object.
$year 1.0.0+ Returns the year as a number. Accepts a single argument expression resolving a moment object.
$format 1.0.0+ Returns the date as a string formated according to the specified format. Accepts two expressions as arguments: the first argument must resolve to a moment object and the second (optional) must resolve to a string. See moment's documentation for more information.
$parse 1.0.0+ Create a moment date object from the specified expression. Accepts a single argument expression resolving a number or a string.
$valueOf 1.1.0+ Returns the number of milliseconds since the Unix Epoch. Accepts a single argument expression resolving a moment date object.
$diff 1.2.0+ Returns the difference between two dates. Accepts four expression arguments. First and second arguments must resolve to a moment date object. Third argument must resolve a string (possible values are 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'. Default is 'milliseconds'). Fourth argument must resolve a boolean for floating point number (default : false).

Example : 

_([
    { date: '1987-04-30 12:15:59.123' }
]).$project({
    date: { $parse: '$date' }
}).$project : {
    dayOfMonth: { $dayOfMonth: '$date' },
    dayOfWeek: { $dayOfWeek: '$date' },
    dayOfYear: { $dayOfYear: '$date' },
    hour: { $hour: '$date' },
    millisecond: { $millisecond: '$date' },
    minute: { $minute: '$date' },
    month: { $month: '$date' },
    second: { $second: '$date' },
    week: { $week: '$date' },
    year: { $year: '$date' },
    format: { $format: ['$date', 'LT' ] }
}).first(); 
// => { 
//    "dayOfMonth": 30, 
//    "dayOfWeek": 4, 
//    "dayOfYear": 120, 
//    "hour": 12, 
//    "millisecond": 123, 
//    "minute": 15, 
//    "month": 3, 
//    "second": 59,  
//    "week": 18, 
//    "year": 1987, 
//    "format": "12:15 PM" 
// };

Quick reference

  • Stages :
    • $match : _(collection).$match({ <query1>, <query2>, ... })
    • $project: _(collection).$project({ <spec1>, <spec2>, ... ); with specification formatted like <field>: <expression>
    • $group : _(collection).$group({ _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... );.
    • $objectify: _(collection).$objectify({ _key: <expression>, _value:<expression> });
  • Expressions :
    • Field paths: <field>: '$path.to.attribute'
    • Literals: <field>: 'toto' or <field>: { $literal: 'toto'}
    • Accumulators (used by $group stage only):
      • $sum: <expression>
      • $avg: <expression>
      • $first: <expression>
      • $last: <expression>
      • $max: <expression>
      • $min: <expression>
      • $addToSet: <expression>
      • $fn: <function(items)>
    • Arithmetic operators :
      • $add: [ <expr1>, <expr2>, ... ]
      • $divide: [ <expr1>, <expr2> ]
      • $mod: [ <expr1>, <expr2> ]
      • $multiply: [ <expr1>, <expr2>, ... ]
      • $subtract: [ <expr1>, <expr2> ]
    • Boolean operators:
      • $and: [ <expr1>, <expr2>, ... ]
      • $or: [ <expr1>, <expr2>, ... ]
      • $not: <expression>
    • Comparison operators:
      • $eq: [ <expr1>, <expr2> ]
      • $ne: [ <expr1>, <expr2> ]
      • $gt: [ <expr1>, <expr2> ]
      • $gte: [ <expr1>, <expr2> ]
      • $lt: [ <expr1>, <expr2> ]
      • $lte: [ <expr1>, <expr2> ]
    • Array operators:
      • $size: <expression>
    • String operators
      • $format: [ <expr1>, <expr2>, ... ] or $format: <expression> ]
      • $substr: [ <expr1>, <expr2>, <expr3> ]
      • $toLower: <expression>
      • $toUpper: <expression>
    • Date operators
      • $dayOfMonth: <expression>
      • $dayOfWeek: <expression>
      • $dayOfYear: <expression>
      • $hour: <expression>
      • $millisecond: <expression>
      • $minute: <expression>
      • $month: <expression>
      • $second: <expression>
      • $week: <expression>
      • $year: <expression>
      • $format: [ <expr1>, <expr2> ]
      • $format: <expression>
      • $valueOf: <expression>
      • $diff: [ <expr1>, <expr2> [, <expr3> [, <expr4> ]] ]
    • General purpose operators:
      • $fn: <function(item)>

Contributions

Feel free to fork and improve underscore.aggregate in any way your want.

If you feel that the community will benefit from your changes, please send a pull request :

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add documentation if necessary.
  • Add tests for it. This is important so I don't break it in a future version unintentionally (run tests using grunt test command).
  • Send a pull request to the develop branch.

Credits

Largely inspired by the great work of mongodb's community.

License

Released under the terms of MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.