Pipeline Terminator - Aggregation
In this section, we’ll explore how to use aggregation terminators in Pipeline. We’ll use items model for this example.
var schema = { id: { type: NIMBUSDB_DATA_TYPE.INTEGER, const: NIMBUSDB_CONSTRAINT.PRIMARY_KEY }, name: NIMBUSDB_DATA_TYPE.STRING, price: { type: NIMBUSDB_DATA_TYPE.NUMBER, validator: function(data, value) { return value >= 0; }, default_value: 0 }, is_locked: { type: NIMBUSDB_DATA_TYPE.BOOLEAN, const: NIMBUSDB_CONSTRAINT.OPTIONAL, default_value: false }, color: NIMBUSDB_DATA_TYPE.STRING};
items = new NimbusDBModel("global", "items", schema, [ { id: 1, name: "Apple", price: 5, color: "red" }, { id: 2, name: "Banana", price: 7.2, color: "yellow" }, { id: 3, name: "Cherry", price: 15, color: "red" }, { id: 4, name: "Date", price: 12.5, color: "green" }, { id: 5, name: "Elderberry", price: 8, color: "purple" }, { id: 6, name: "Fig", price: 10, color: "purple" }, { id: 7, name: "Grape", price: 6, color: "purple" }, { id: 8, name: "Honeydew", price: 9, color: "green" }, { id: 9, name: "Kiwi", price: 4, color: "green" }, { id: 10, name: "Lemon", price: 3, color: "yellow" }]);Numerical Aggregation
Section titled “Numerical Aggregation”The result of the pipeline operations must be an array of numbers (except for .count()) if you want to use the numerical aggregation terminators.
Count Any
Section titled “Count ”The count terminator returns the number of elements in the pipeline result.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of strings return data.name; });
var result = items_pl.count();show_debug_message($"Result: {result}");
// Result: 3Sum Array of Numbers
Section titled “Sum ”The sum terminator returns the sum of the numeric values in the pipeline result.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of numbers return data.price; });
var result = items_pl.sum();show_debug_message($"Result: {result}");
// Result: 35.5Average Array of Numbers
Section titled “Average ”The avg terminator returns the average of the numeric values in the pipeline result.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of numbers return data.price; });
var result = items_pl.avg();show_debug_message($"Result: {result}");
// Result: 11.833Min Array of Numbers
Section titled “Min ”The min terminator returns the minimum value in the pipeline result.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of numbers return data.price; });
var result = items_pl.min();show_debug_message($"Result: {result}");
// Result: 8Max Array of Numbers
Section titled “Max ”The max terminator returns the maximum value in the pipeline result.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of numbers return data.price; });
var result = items_pl.max();show_debug_message($"Result: {result}");
// Result: 15Grouping
Section titled “Grouping”Group by Array
Section titled “Group by ”The group_by terminator groups the pipeline data by a column value.
var items_pl = items.pipe().isolate() .limit(6); // limit the result to 6 items
// (1) using a column namevar result = items_pl.group_by("color");show_debug_message($"Result: {result}");
// Result: {// red: [// { id: 1, name: "Apple", price: 5, color: "red" },// { id: 3, name: "Cherry", price: 15, color: "red" }// ],// yellow: [// { id: 2, name: "Banana", price: 7.2, color: "yellow" }// ],// green: [// { id: 4, name: "Date", price: 12.5, color: "green" }// ],// purple: [// { id: 5, name: "Elderberry", price: 8, color: "purple" },// { id: 6, name: "Fig", price: 10, color: "purple" }// ]// }
// (2) using mapper functionvar result = items_pl.group_by(function(data, index) { return data.price <= 5 ? "cheap_items" : (data.price <= 10 ? "normal_items" : "expensive_items");});show_debug_message($"Result: {result}");
// Result: {// cheap_items: [// { id: 1, name: "Apple", price: 5, color: "red" }// ],// normal_items: [// { id: 2, name: "Banana", price: 7.2, color: "yellow" },// { id: 5, name: "Elderberry", price: 8, color: "purple" },// { id: 6, name: "Fig", price: 10, color: "purple" }// ],// expensive_items: [// { id: 3, name: "Cherry", price: 15, color: "red" },// { id: 4, name: "Date", price: 12.5, color: "green" }// ]// }Group Count Array
Section titled “Group Count ”The group_count terminator groups the pipeline data by a mapper function and counts the number of elements in each group.
var items_pl = items.pipe().isolate() .limit(6); // limit the result to 6 items
var result = items_pl.group_by(function(data, index) { return data.price <= 5 ? "cheap_items_count" : (data.price <= 10 ? "normal_items_count" : "expensive_items_count");});show_debug_message($"Result: {result}");
// Result: {// cheap_items_count: 1,// normal_items_count: 3,// expensive_items_count: 2// }Group Reduce Array
Section titled “Group Reduce ”The group_reduce terminator groups the pipeline data by a mapper function and reduces each group using a reducer function.
var items_pl = items.pipe().isolate() .limit(6); // limit the result to 6 items
var result = items_pl.group_reduce( function(data, index) { // mapper function return data.price <= 5 ? "cheap_item" : (data.price <= 10 ? "normal_item" : "expensive_item"); }, function(acc, data, index) { // reducer function acc.count++; acc.total_price += data.price;
return acc; }, { // initial accumulator for each group count: 0, total_price: 0 });show_debug_message($"Result: {result}");
// Result: {// cheap_item : {// count : 1,// total_price : 5// },// normal_item : {// count : 3,// total_price : 25.20// },// expensive_item : {// count : 2,// total_price : 27.50// }// }Partitioning
Section titled “Partitioning”Partition Array
Section titled “Partition ”The partition terminator partitions the pipeline data into a tuple of 2 arrays:
- index
0: elements that match, and - index
1: elements that don’t match.
var items_pl = items.pipe().isolate() .limit(6); // limit the result to 6 items
var result = items_pl.partition(function(data, index) { return data.price < 8;});show_debug_message($"Result: {result}");
// Result: [// [// { id: 1, name: "Apple", price: 5, color: "red" },// { id: 2, name: "Banana", price: 7.2, color: "yellow" }// ],// [// { id: 3, name: "Cherry", price: 15, color: "red" },// { id: 4, name: "Date", price: 12.5, color: "green" },// { id: 5, name: "Elderberry", price: 8, color: "purple" },// { id: 6, name: "Fig", price: 10, color: "purple" }// ]// ]References
Section titled “References”Pipeline.avg()
Section titled “Pipeline.avg()”Executes the pipeline at the given cursor position and returns the average of numeric values.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static avg( _pos?: int, _options?: NimbusDBPipelineTerminalOptions ): number | undefined;}Parameters
Section titled “Parameters”- Type:
int - Default:
-1(the last step) - The cursor position used as the end of the pipeline operations.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the avg operation.
Returns
Section titled “Returns”- Type:
number|undefined - The average, or
undefinedif unavailable.
Pipeline.count()
Section titled “Pipeline.count()”Executes the pipeline at the given cursor position and returns the number of elements.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static count( _pos?: int, _options?: NimbusDBPipelineTerminalOptions ): int | undefined;}Parameters
Section titled “Parameters”- Type:
int - Default:
-1(the last step) - The cursor position used as the end of the pipeline operations.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the count operation.
Returns
Section titled “Returns”- Type:
int - The element count, or
undefinedif unavailable.
Pipeline.group_by()
Section titled “Pipeline.group_by()”Executes the pipeline at the given cursor position and groups the pipeline data by a column value, returning a keyed object of arrays.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static group_by( _column: string, _options?: NimbusDBPipelineTerminalOptions ): { [key: string]: any[]; };}Parameters
Section titled “Parameters”_column
Section titled “_column”- Type:
string - The column name to group by.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the group_by operation.
Returns
Section titled “Returns”- Type:
{ [key: string]: any[]; } - The keyed object of arrays.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static group_by( _pos: int, _column: string, _options?: NimbusDBPipelineTerminalOptions ): { [key: string]: any[]; };}Parameters
Section titled “Parameters”- Type:
int - The cursor position used as the end of the pipeline operations.
_column
Section titled “_column”- Type:
string - The column name to group by.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the group_by operation.
Returns
Section titled “Returns”- Type:
{ [key: string]: any[]; } - The keyed object of arrays.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static group_by( _key_fn: (data: any, index: int) => string, _options?: NimbusDBPipelineTerminalOptions ): { [key: string]: any[]; };}Parameters
Section titled “Parameters”_key_fn
Section titled “_key_fn”- Type:
(data: any, index: int) => string- Parameters:
data: The current data element.index: The current index.
- Returns: The key to group by.
- Parameters:
- The function to use to group by.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the group_by operation.
Returns
Section titled “Returns”- Type:
{ [key: string]: any[]; } - The keyed object of arrays.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static group_by( _pos: int, _key_fn: (data: any, index: int) => string, _options?: NimbusDBPipelineTerminalOptions ): { [key: string]: any[]; };}Parameters
Section titled “Parameters”- Type:
int - The cursor position used as the end of the pipeline operations.
_key_fn
Section titled “_key_fn”- Type:
(data: any, index: int) => string- Parameters:
data: The current data element.index: The current index.
- Returns: The key to group by.
- Parameters:
- The function to use to group by.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the group_by operation.
Returns
Section titled “Returns”- Type:
{ [key: string]: any[]; } - The keyed object of arrays.
Pipeline.group_count()
Section titled “Pipeline.group_count()”Executes the pipeline at the given cursor position, groups and counts elements using a key function, returning a keyed object of counts.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static group_count( _func: (data: any, index: int) => any, _options?: NimbusDBPipelineTerminalOptions ): { [key: string]: int; };}Parameters
Section titled “Parameters”- Type:
(data: any, index: int) => any- Parameters:
data: The current data element.index: The current index.
- Returns: The key to group by.
- Parameters:
- The function to use to group by.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the group_count operation.
Returns
Section titled “Returns”- Type:
{ [key: string]: int; } - The keyed object of counts.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static group_count( _pos: int, _func: (data: any, index: int) => any, _options?: NimbusDBPipelineTerminalOptions ): { [key: string]: int; };}Parameters
Section titled “Parameters”- Type:
int - The cursor position used as the end of the pipeline operations.
- Type:
(data: any, index: int) => any- Parameters:
data: The current data element.index: The current index.
- Returns: The key to group by.
- Parameters:
- The function to use to group by.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the group_count operation.
Returns
Section titled “Returns”- Type:
{ [key: string]: int; } - The keyed object of counts.
Pipeline.group_reduce()
Section titled “Pipeline.group_reduce()”Executes the pipeline at the given cursor position, groups the pipeline data by a key function and reduces each group using an accumulator.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static group_reduce( _key_func: (data: any, index: int) => string, _reducer: (acc: any, data: any, index: int) => any, _acc?: any, _options?: NimbusDBPipelineTerminalOptions ): { [key: string]: any; };}Parameters
Section titled “Parameters”_key_func
Section titled “_key_func”- Type:
(data: any, index: int) => string- Parameters:
data: The current data element.index: The current index.
- Returns: The key to group by.
- Parameters:
- The function to use to group by.
_reducer
Section titled “_reducer”- Type:
(acc: any, data: any, index: int) => any- Parameters:
acc: The current accumulator.data: The current data element.index: The current index.
- Returns: The reduced value.
- Parameters:
- The function to use to reduce each group.
- Type:
any - The initial accumulator value.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the group_reduce operation.
Returns
Section titled “Returns”- Type:
{ [key: string]: any; } - The keyed object of reduced values.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static group_reduce( _pos: int, _key_func: (data: any, index: int) => string, _reducer: (acc: any, data: any, index: int) => any, _acc?: any, _options?: NimbusDBPipelineTerminalOptions ): { [key: string]: any; };}Parameters
Section titled “Parameters”- Type:
int - The cursor position used as the end of the pipeline operations.
_key_func
Section titled “_key_func”- Type:
(data: any, index: int) => string- Parameters:
data: The current data element.index: The current index.
- Returns: The key to group by.
- Parameters:
- The function to use to group by.
_reducer
Section titled “_reducer”- Type:
(acc: any, data: any, index: int) => any- Parameters:
acc: The current accumulator.data: The current data element.index: The current index.
- Returns: The reduced value.
- Parameters:
- The function to use to reduce each group.
- Type:
any - The initial accumulator value.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the group_reduce operation.
Returns
Section titled “Returns”- Type:
{ [key: string]: any; } - The keyed object of reduced values.
Pipeline.max()
Section titled “Pipeline.max()”Executes the pipeline at the given cursor position and returns the maximum numeric value.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static max( _pos?: int, _options?: NimbusDBPipelineTerminalOptions ): number | undefined;}Parameters
Section titled “Parameters”- Type:
int - Default:
-1(the last step) - The cursor position used as the end of the pipeline operations.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the max operation.
Returns
Section titled “Returns”- Type:
number|undefined - The maximum value, or
undefinedif unavailable.
Pipeline.min()
Section titled “Pipeline.min()”Executes the pipeline at the given cursor position and returns the minimum numeric value.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static min( _pos?: int, _options?: NimbusDBPipelineTerminalOptions ): number | undefined;}Parameters
Section titled “Parameters”- Type:
int - Default:
-1(the last step) - The cursor position used as the end of the pipeline operations.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the min operation.
Returns
Section titled “Returns”- Type:
number|undefined - The minimum value, or
undefinedif unavailable.
Pipeline.partition()
Section titled “Pipeline.partition()”Executes the pipeline at the given cursor position and splits the pipeline data into two arrays:
- index
0: elements that match, and - index
1: elements that don’t match.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static partition( _func: (data: any, index: int) => boolean, _options?: NimbusDBPipelineTerminalOptions ): [ any[], any[] ];}Parameters
Section titled “Parameters”- Type:
(data: any, index: int) => boolean- Parameters:
data: The current data element.index: The current index.
- Returns:
trueif the element matches,falseotherwise.
- Parameters:
- The function to use to partition the data.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the partition operation.
Returns
Section titled “Returns”- Type:
[ any[], any[] ] - The partitioned data. Index
0contains elements that match, and index1contains elements that don’t match.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static partition( _pos: int, _func: (data: any, index: int) => boolean, _options?: NimbusDBPipelineTerminalOptions ): [ any[], any[] ];}Parameters
Section titled “Parameters”- Type:
int - The cursor position used as the end of the pipeline operations.
- Type:
(data: any, index: int) => boolean- Parameters:
data: The current data element.index: The current index.
- Returns:
trueif the element matches,falseotherwise.
- Parameters:
- The function to use to partition the data.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the partition operation.
Returns
Section titled “Returns”- Type:
[ any[], any[] ] - The partitioned data. Index
0contains elements that match, and index1contains elements that don’t match.
Pipeline.sum()
Section titled “Pipeline.sum()”Executes the pipeline at the given cursor position and returns the sum of numeric values.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static sum( _pos?: int, _options?: NimbusDBPipelineTerminalOptions ): number | undefined;}Parameters
Section titled “Parameters”- Type:
int - Default:
-1(the last step) - The cursor position used as the end of the pipeline operations.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the sum operation.
Returns
Section titled “Returns”- Type:
number|undefined - The sum, or
undefinedif unavailable.