$integral

Calculates the area under a curve based on specified range of documents sorted by a specific field, useful for cumulative analysis.

Syntax

{ $integral: { input: <expression>, unit: <timeWindow> } }

Parameters

inputobjectrequired

Field in documents to use for integral calculation

unitstringrequired

Specified time unit for the integral calculation

Examples

Calculate integral of total sales

Compute integral of sales from first to current document within company partition

Query:

db.stores.aggregate([{ $match: { "company": { $in: ["First Up Consultants"] } } }, { $setWindowFields: { "partitionBy": "$company", "sortBy": { "storeOpeningDate": 1 }, "output": { "salesIntegral": { "$integral": { "input": "$sales.revenue", "unit": "hour" }, "window": { "range": ["unbounded", "current"], "unit": "hour" } } } } }])

Output:

Stores with integral calculation of revenue over time in hourly units

Cumulative revenue integration

Calculate area under revenue curve for business analysis

Query:

db.financial.aggregate([{ $setWindowFields: { "partitionBy": "$department", "sortBy": { "month": 1 }, "output": { "cumulativeRevenue": { "$integral": { "input": "$monthlyRevenue", "unit": "month" } } } } }])

Output:

Financial records with integrated revenue calculations over monthly periods

Related