jstests/aggregation/extras/window_function_helpers.js
/**
 * Runs a pipeline containing a $group that computes the window-function equivalent using the
 * given partition key, accumulator, index in the current partition, and bounds.
 *
 * The bounds are an inclusive range [lower, upper], specified as values relative to the current
 * offset in the partition. They can be numeric, "current", or "unbounded".
 *
 * The skip/limit values are calculated from the given bounds and the current index.
 *
 * 'accumSpec' is the specification of accumulator being tested and is an object of the form
 * {accumulatorName: <accumulator arguments>}.
 *
 * 'defaultValue' is used in cases when the skip/limit combination result in $group not getting any
 * documents. The most likely scenario is that the window has gone off the side of the partition.
 *
 * Note that this function assumes that the data in 'coll' has been seeded with the documents from
 * the seedWithTickerData() method above.
 */
Runs a pipeline containing a $group that computes the window-function equivalent using the
given partition key, accumulator, index in the current partition, and bounds.

The bounds are an inclusive range [lower, upper], specified as values relative to the current
offset in the partition. They can be numeric, "current", or "unbounded".

The skip/limit values are calculated from the given bounds and the current index.

'accumSpec' is the specification of accumulator being tested and is an object of the form
{accumulatorName: }.

'defaultValue' is used in cases when the skip/limit combination result in $group not getting any
documents. The most likely scenario is that the window has gone off the side of the partition.

Note that this function assumes that the data in 'coll' has been seeded with the documents from
the seedWithTickerData() method above.
Runs a pipeline containing a $group that computes the window - function equivalent using the given partition key accumulator index in the current partition and bounds . The bounds are an inclusive range [ lower upper ] specified as values relative to the current offset in the partition . They can be numeric " current " or " unbounded " . The skip / limit values are calculated from the given bounds and the current index . ' accumSpec ' is the specification of accumulator being tested and is an object of the form { accumulatorName : } . ' defaultValue ' is used in cases when the skip / limit combination result in $group not getting any documents . The most likely scenario is that the window has gone off the side of the partition . Note that this function assumes that the data in ' coll ' has been seeded with the documents from the seedWithTickerData () method above .
function computeAsGroup({
    coll,
    partitionKey,
    accumSpec,
    bounds,
    indexInPartition,
    defaultValue = null,
}) {
    const skip = calculateSkip(bounds[0], indexInPartition);
    const limit = calculateLimit(bounds[0], bounds[1], indexInPartition);
    if (skip < 0 || limit <= 0)
        return defaultValue;
    let prefixPipe = [{$match: partitionKey}, {$sort: {_id: 1}}, {$skip: skip}];
    if (limit != "unbounded")
        prefixPipe = prefixPipe.concat([{$limit: limit}]);
    const result =
        coll.aggregate(prefixPipe.concat([{$group: {_id: null, res: accumSpec}}])).toArray();
    if (result.length == 0) {
        return defaultValue;
    }
    return result[0].res;
}
function computeAsGroup ( { coll , partitionKey , accumSpec , bounds , indexInPartition , defaultValue = null , } ) { const skip = calculateSkip ( bounds [ 0 ] , indexInPartition ) ; const limit = calculateLimit ( bounds [ 0 ] , bounds [ 1 ] , indexInPartition ) ; if ( skip < 0 || limit <= 0 ) return defaultValue ; let prefixPipe = [ { $match : partitionKey } , { $sort : { _id : 1 } } , { $skip : skip } ] ; if ( limit != "unbounded" ) prefixPipe = prefixPipe . concat ( [ { $limit : limit } ] ) ; const result = coll . aggregate ( prefixPipe . concat ( [ { $group : { _id : null , res : accumSpec } } ] ) ) . toArray ( ) ; if ( result . length == 0 ) { return defaultValue ; } return result [ 0 ] . res ; }
Runs a pipeline containing a $group that computes the window-function equivalent using the
 given partition key, accumulator, index in the current partition, and bounds.
Runs a pipeline containing a $group that computes the window - function equivalent using the given partition key accumulator index in the current partition and bounds .
// Only attach a $limit if there's a numeric upper bound (or "current"), since "unbounded" // implies an infinite limit. // If the window is completely off the edge of the right side of the partition, return null.