Interface WeivDataAggregate<CItem, ReservedCItem>

Returned aggregation cursor of WeivData. Add filters, lookups (joins) and more when aggregating data.

Welcome to weivData.aggregate function of weiv-data library. This feature/function allows you to perform calculations on your database collections data. You can use aggregate with any collection! Read documentation to learn from examples.

Note: Unlike wix-data in weiv-data order of methods matters, if you put filter method at the begining of aggregation it'll first filter the documents and then pass to next stage, if you pass filter method after grouping then it'll act like wix-data's having method and filter the results after grouping stage. There are some optimizations made about ordering for example .limit and .skip order doesn't matter but it's always better to keep the methods order in a meaningful way so you can control the aggregation more and even optimize it with this control.

You can also use .addStage method to add any type of stage to aggregation that mongodb driver allows in this way you can mix the default methods which works similar/same with wix-data and also native methods that mongodb provides!

// Here is a quick example from our internal systems where we can calculate average CPU scores for each cluster location:

import weivData from '@exweiv/weiv-data';

const result = await weivData.aggreagte('ExWeiv/Istanbul').avg('cpuScore').run();
console.log(result);
interface WeivDataAggregate<CItem, ReservedCItem> {
    ascending(propertyName: Extract<keyof CItem, string>): WeivDataAggregate<CItem, ReservedCItem>;
    avg<K, P>(propertyName: K, projectedName?: P): WeivDataAggregate<Pick<UpdateCItem<UpdateCItemIDToNull<CItem>, ProjectedName<P, K, "Avg">, number>, "_id" extends keyof UpdateCItemIDToNull<CItem>
        ? ProjectedName<P, K, "Avg"> | "_id"
        : ProjectedName<P, K, "Avg">>, ReservedCItem>;
    count(): WeivDataAggregate<UpdateCItem<CItem, "count", number>, ReservedCItem>;
    descending(propertyName: Extract<keyof CItem, string>): WeivDataAggregate<CItem, ReservedCItem>;
    filter(filter: WeivDataFilter<Extract<keyof CItem, string>>): WeivDataAggregate<CItem, ReservedCItem>;
    group<K>(...propertyName: K[]): WeivDataAggregate<UpdatedCItemWithGroupedID<ReservedCItem, K, CItem>, ReservedCItem>;
    limit(limit: number): WeivDataAggregate<CItem, ReservedCItem>;
    max<K, P>(propertyName: K, projectedName?: P): WeivDataAggregate<Pick<UpdateCItem<UpdateCItemIDToNull<CItem>, ProjectedName<P, K, "Max">, number>, "_id" extends keyof UpdateCItemIDToNull<CItem>
        ? "_id" | ProjectedName<P, K, "Max">
        : ProjectedName<P, K, "Max">>, ReservedCItem>;
    min<K, P>(propertyName: K, projectedName?: P): WeivDataAggregate<Pick<UpdateCItem<UpdateCItemIDToNull<CItem>, ProjectedName<P, K, "Min">, number>, "_id" extends keyof UpdateCItemIDToNull<CItem>
        ? "_id" | ProjectedName<P, K, "Min">
        : ProjectedName<P, K, "Min">>, ReservedCItem>;
    run(options?: WeivDataAggregateRunOptions): Promise<WeivDataAggregateResult<CItem>>;
    skip(skip: number): WeivDataAggregate<CItem, ReservedCItem>;
    stage<NewCItem>(...stage: PipelineStage[]): WeivDataAggregate<NewCItem & CItem, ReservedCItem>;
    sum<K, P>(propertyName: K, projectedName?: P): WeivDataAggregate<Pick<UpdateCItem<UpdateCItemIDToNull<CItem>, ProjectedName<P, K, "Sum">, number>, "_id" extends keyof UpdateCItemIDToNull<CItem>
        ? "_id" | ProjectedName<P, K, "Sum">
        : ProjectedName<P, K, "Sum">>, ReservedCItem>;
}

Type Parameters

  • CItem
  • ReservedCItem

Methods

  • Type Parameters

    • K extends string
    • P extends undefined | string = undefined

    Parameters

    • propertyName: K

      The property in which to find the average value.

    • OptionalprojectedName: P

      The name of the property in the aggregation results containing the average value.

    Returns WeivDataAggregate<Pick<UpdateCItem<UpdateCItemIDToNull<CItem>, ProjectedName<P, K, "Avg">, number>, "_id" extends keyof UpdateCItemIDToNull<CItem>
        ? ProjectedName<P, K, "Avg"> | "_id"
        : ProjectedName<P, K, "Avg">>, ReservedCItem>

    A WeivDataAggregate cursor representing the refined aggregation.

    Refines a WeivDataAggregate to only contain the average value from each aggregation group.

  • Type Parameters

    • K extends string
    • P extends undefined | string = undefined

    Parameters

    • propertyName: K

      The property in which to find the maximum value.

    • OptionalprojectedName: P

      The name of the property in the aggregation results containing the maximum value.

    Returns WeivDataAggregate<Pick<UpdateCItem<UpdateCItemIDToNull<CItem>, ProjectedName<P, K, "Max">, number>, "_id" extends keyof UpdateCItemIDToNull<CItem>
        ? "_id" | ProjectedName<P, K, "Max">
        : ProjectedName<P, K, "Max">>, ReservedCItem>

    A WeivDataAggregate cursor representing the refined aggregation.

    Refines a WeivDataAggregate to only contain the maximum value from each aggregation group.

  • Type Parameters

    • K extends string
    • P extends undefined | string = undefined

    Parameters

    • propertyName: K

      The property in which to find the minimum value.

    • OptionalprojectedName: P

      The name of the property in the aggregation results containing the minimum value.

    Returns WeivDataAggregate<Pick<UpdateCItem<UpdateCItemIDToNull<CItem>, ProjectedName<P, K, "Min">, number>, "_id" extends keyof UpdateCItemIDToNull<CItem>
        ? "_id" | ProjectedName<P, K, "Min">
        : ProjectedName<P, K, "Min">>, ReservedCItem>

    A WeivDataAggregate cursor representing the refined aggregation.

    Refines a WeivDataAggregate to only contain the minimum value from each aggregation group.

  • Type Parameters

    • K extends string
    • P extends undefined | string = undefined

    Parameters

    • propertyName: K

      The property in which to find the sum.

    • OptionalprojectedName: P

      The name of the property in the aggregation results containing the sum.

    Returns WeivDataAggregate<Pick<UpdateCItem<UpdateCItemIDToNull<CItem>, ProjectedName<P, K, "Sum">, number>, "_id" extends keyof UpdateCItemIDToNull<CItem>
        ? "_id" | ProjectedName<P, K, "Sum">
        : ProjectedName<P, K, "Sum">>, ReservedCItem>

    A WeivDataAggregate cursor representing the refined aggregation.

    Refines a WeivDataAggregate to contain the sum from each aggregation group.