Package com.mongodb.client.model
Class WindowOutputFields
java.lang.Object
com.mongodb.client.model.WindowOutputFields
Builders for window output fields used in the
$setWindowFields
pipeline stage
of an aggregation pipeline. Each windowed computation is a triple:
- A window function. Some functions require documents in a window to be sorted
(see
sortBy
inAggregates.setWindowFields(Object, Bson, Iterable)
). - An optional window, a.k.a. frame.
Specifying
null
window is equivalent to specifying an unbounded window, i.e., a window with both ends specified asWindows.Bound.UNBOUNDED
. Some window functions, e.g.,derivative(String, Object, Window)
, require an explicit unbounded window instead ofnull
. - A path to an output field to be computed by the window function over the window.
- Since:
- 4.3
- MongoDB documentation
- Field paths
- Since server release
- 5.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <TExpression>
WindowOutputFieldBuilds a window output field that adds the evaluation results of theexpression
over thewindow
to a BSONArray
and excludes duplicates.static <TExpression>
WindowOutputFieldBuilds a window output field of the average of the evaluation results of theexpression
over thewindow
.static <OutExpression>
WindowOutputFieldBuilds a window output field of the evaluation result of theoutExpression
against the bottom document in thewindow
sorted according to the providedsortBy
specification.static <OutExpression,
NExpression>
WindowOutputFieldbottomN
(String path, Bson sortBy, OutExpression outExpression, NExpression nExpression, Window window) Builds a window output field of a BSONArray
of evaluation results of theoutExpression
against the bottomN
documents in thewindow
sorted according to the providedsortBy
specification, whereN
is the positive integral value of thenExpression
.static WindowOutputField
Builds a window output field of the number of documents in thewindow
.static <TExpression>
WindowOutputFieldcovariancePop
(String path, TExpression expression1, TExpression expression2, Window window) Builds a window output field of the population covariance between the evaluation results of the two expressions over thewindow
.static <TExpression>
WindowOutputFieldcovarianceSamp
(String path, TExpression expression1, TExpression expression2, Window window) Builds a window output field of the sample covariance between the evaluation results of the two expressions over thewindow
.static WindowOutputField
Builds a window output field of the dense rank of each document in its partition.static <TExpression>
WindowOutputFieldderivative
(String path, TExpression expression, Window window) Builds a window output field of the time derivative by subtracting the evaluation result of theexpression
against the last document and the first document in thewindow
and dividing it by the difference in the values of thesortBy
field of the respective documents.static WindowOutputField
documentNumber
(String path) Builds a window output field of the order number of each document in its partition.static <TExpression>
WindowOutputFieldexpMovingAvg
(String path, TExpression expression, double alpha) Builds a window output field of the exponential moving average of the evaluation results of theexpression
over the half-bounded window [Windows.Bound.UNBOUNDED
,Windows.Bound.CURRENT
], withalpha
representing the degree of weighting decrease.static <TExpression>
WindowOutputFieldexpMovingAvg
(String path, TExpression expression, int n) Builds a window output field of the exponential moving average of the evaluation results of theexpression
over a window that includesn
- 1 documents preceding the current document and the current document, with more weight on documents closer to the current one.static <TExpression>
WindowOutputFieldBuilds a window output field of the evaluation result of theexpression
against the first document in thewindow
.static <InExpression,
NExpression>
WindowOutputFieldBuilds a window output field of a BSONArray
of evaluation results of theinExpression
against the firstN
documents in thewindow
, whereN
is the positive integral value of thenExpression
.static <TExpression>
WindowOutputFieldBuilds a window output field of the approximate integral of a function that maps values of thesortBy
field to evaluation results of theexpression
against the same document.static <TExpression>
WindowOutputFieldBuilds a window output field of the evaluation result of theexpression
against the last document in thewindow
.static <InExpression,
NExpression>
WindowOutputFieldBuilds a window output field of a BSONArray
of evaluation results of theinExpression
against the lastN
documents in thewindow
, whereN
is the positive integral value of thenExpression
.static <TExpression>
WindowOutputFieldlinearFill
(String path, TExpression expression) static <TExpression>
WindowOutputFieldBuilds a window output field of the last observed non-Null
evaluation result of theexpression
.static <TExpression>
WindowOutputFieldBuilds a window output field of the largest of the evaluation results of theexpression
over thewindow
.static <InExpression,
NExpression>
WindowOutputFieldBuilds a window output field of a BSONArray
ofN
largest evaluation results of theinExpression
over thewindow
, whereN
is the positive integral value of thenExpression
.static <TExpression>
WindowOutputFieldBuilds a window output field of the smallest of the evaluation results of theexpression
over thewindow
.static <InExpression,
NExpression>
WindowOutputFieldBuilds a window output field of a BSONArray
ofN
smallest evaluation results of theinExpression
over thewindow
, whereN
is the positive integral value of thenExpression
.static WindowOutputField
Creates a windowed output field from a document field in situations when there is no builder method that better satisfies your needs.static <TExpression>
WindowOutputFieldBuilds a window output field that adds the evaluation results of theexpression
over thewindow
to a BSONArray
.static WindowOutputField
Builds a window output field of the rank of each document in its partition.static <TExpression>
WindowOutputFieldBuilds a window output field of the evaluation result of theexpression
for the document whose position is shifted by the given amount relative to the current document.static <TExpression>
WindowOutputFieldBuilds a window output field of the population standard deviation of the evaluation results of theexpression
over thewindow
.static <TExpression>
WindowOutputFieldstdDevSamp
(String path, TExpression expression, Window window) Builds a window output field of the sample standard deviation of the evaluation results of theexpression
over thewindow
.static <TExpression>
WindowOutputFieldBuilds a window output field of the sum of the evaluation results of theexpression
over thewindow
.static <TExpression>
WindowOutputFieldtimeDerivative
(String path, TExpression expression, Window window, MongoTimeUnit unit) static <TExpression>
WindowOutputFieldtimeIntegral
(String path, TExpression expression, Window window, MongoTimeUnit unit) static <OutExpression>
WindowOutputFieldBuilds a window output field of the evaluation result of theoutExpression
against the top document in thewindow
sorted according to the providedsortBy
specification.static <OutExpression,
NExpression>
WindowOutputFieldBuilds a window output field of a BSONArray
of evaluation results of theoutExpression
against the topN
documents in thewindow
sorted according to the providedsortBy
specification, whereN
is the positive integral value of thenExpression
.
-
Method Details
-
of
Creates a windowed output field from a document field in situations when there is no builder method that better satisfies your needs. This method cannot be used to validate the document field syntax.Example
The following code creates two functionally equivalent window output fields, though they may not be equal.Window pastWeek = Windows.timeRange(-1, MongoTimeUnit.WEEK, Windows.Bound.CURRENT); WindowOutputField pastWeekExpenses1 = WindowOutputFields.sum("pastWeekExpenses", "$expenses", pastWeek); WindowOutputField pastWeekExpenses2 = WindowOutputFields.of( new BsonField("pastWeekExpenses", new Document("$sum", "$expenses") .append("window", pastWeek.toBsonDocument())));
- Parameters:
windowOutputField
- A document field representing the required windowed output field.- Returns:
- The constructed windowed output field.
-
sum
public static <TExpression> WindowOutputField sum(String path, TExpression expression, @Nullable Window window) Builds a window output field of the sum of the evaluation results of theexpression
over thewindow
.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $sum
-
avg
public static <TExpression> WindowOutputField avg(String path, TExpression expression, @Nullable Window window) Builds a window output field of the average of the evaluation results of theexpression
over thewindow
.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $avg
-
stdDevSamp
public static <TExpression> WindowOutputField stdDevSamp(String path, TExpression expression, @Nullable Window window) Builds a window output field of the sample standard deviation of the evaluation results of theexpression
over thewindow
.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $stdDevSamp
-
stdDevPop
public static <TExpression> WindowOutputField stdDevPop(String path, TExpression expression, @Nullable Window window) Builds a window output field of the population standard deviation of the evaluation results of theexpression
over thewindow
.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $stdDevPop
-
min
public static <TExpression> WindowOutputField min(String path, TExpression expression, @Nullable Window window) Builds a window output field of the smallest of the evaluation results of theexpression
over thewindow
.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $min
-
minN
public static <InExpression,NExpression> WindowOutputField minN(String path, InExpression inExpression, NExpression nExpression, @Nullable Window window) Builds a window output field of a BSONArray
ofN
smallest evaluation results of theinExpression
over thewindow
, whereN
is the positive integral value of thenExpression
.- Type Parameters:
InExpression
- The type of the input expression.NExpression
- The type of the limiting expression.- Parameters:
path
- The output field path.inExpression
- The input expression.nExpression
- The expression limiting the number of produced values.window
- The window.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
-
max
public static <TExpression> WindowOutputField max(String path, TExpression expression, @Nullable Window window) Builds a window output field of the largest of the evaluation results of theexpression
over thewindow
.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $max
-
maxN
public static <InExpression,NExpression> WindowOutputField maxN(String path, InExpression inExpression, NExpression nExpression, @Nullable Window window) Builds a window output field of a BSONArray
ofN
largest evaluation results of theinExpression
over thewindow
, whereN
is the positive integral value of thenExpression
.- Type Parameters:
InExpression
- The type of the input expression.NExpression
- The type of the limiting expression.- Parameters:
path
- The output field path.inExpression
- The input expression.nExpression
- The expression limiting the number of produced values.window
- The window.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
-
count
Builds a window output field of the number of documents in thewindow
.- Parameters:
path
- The output field path.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $count
-
derivative
public static <TExpression> WindowOutputField derivative(String path, TExpression expression, Window window) Builds a window output field of the time derivative by subtracting the evaluation result of theexpression
against the last document and the first document in thewindow
and dividing it by the difference in the values of thesortBy
field of the respective documents. Other documents in thewindow
have no effect on the computation.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $derivative
-
timeDerivative
public static <TExpression> WindowOutputField timeDerivative(String path, TExpression expression, Window window, MongoTimeUnit unit) Builds a window output field of the time derivative by subtracting the evaluation result of theexpression
against the last document and the first document in thewindow
and dividing it by the difference in the BSONDate
values of thesortBy
field of the respective documents. Other documents in thewindow
have no effect on the computation.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.unit
- The desired time unit for the divisor. Allowed values are:MongoTimeUnit.WEEK
,MongoTimeUnit.DAY
,MongoTimeUnit.HOUR
,MongoTimeUnit.MINUTE
,MongoTimeUnit.SECOND
,MongoTimeUnit.MILLISECOND
.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $derivative
-
integral
public static <TExpression> WindowOutputField integral(String path, TExpression expression, Window window) Builds a window output field of the approximate integral of a function that maps values of thesortBy
field to evaluation results of theexpression
against the same document. The limits of integration match thewindow
bounds. The approximation is done by using the trapezoidal rule.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $integral
-
timeIntegral
public static <TExpression> WindowOutputField timeIntegral(String path, TExpression expression, Window window, MongoTimeUnit unit) Builds a window output field of the approximate integral of a function that maps BSONDate
values of thesortBy
field to evaluation results of theexpression
against the same document. The limits of integration match thewindow
bounds. The approximation is done by using the trapezoidal rule.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.unit
- The desired time unit for the divisor. Allowed values are:MongoTimeUnit.WEEK
,MongoTimeUnit.DAY
,MongoTimeUnit.HOUR
,MongoTimeUnit.MINUTE
,MongoTimeUnit.SECOND
,MongoTimeUnit.MILLISECOND
.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $integral
-
covarianceSamp
public static <TExpression> WindowOutputField covarianceSamp(String path, TExpression expression1, TExpression expression2, @Nullable Window window) Builds a window output field of the sample covariance between the evaluation results of the two expressions over thewindow
.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression1
- The first expression.expression2
- The second expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $covarianceSamp
-
covariancePop
public static <TExpression> WindowOutputField covariancePop(String path, TExpression expression1, TExpression expression2, @Nullable Window window) Builds a window output field of the population covariance between the evaluation results of the two expressions over thewindow
.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression1
- The first expression.expression2
- The second expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $covariancePop
-
expMovingAvg
public static <TExpression> WindowOutputField expMovingAvg(String path, TExpression expression, int n) Builds a window output field of the exponential moving average of the evaluation results of theexpression
over a window that includesn
- 1 documents preceding the current document and the current document, with more weight on documents closer to the current one.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.n
- Must be positive.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $expMovingAvg
-
expMovingAvg
public static <TExpression> WindowOutputField expMovingAvg(String path, TExpression expression, double alpha) Builds a window output field of the exponential moving average of the evaluation results of theexpression
over the half-bounded window [Windows.Bound.UNBOUNDED
,Windows.Bound.CURRENT
], withalpha
representing the degree of weighting decrease.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.alpha
- A parameter specifying how fast weighting decrease happens. A higheralpha
discounts older observations faster. Must belong to the interval (0, 1).- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $expMovingAvg
-
push
public static <TExpression> WindowOutputField push(String path, TExpression expression, @Nullable Window window) Builds a window output field that adds the evaluation results of theexpression
over thewindow
to a BSONArray
. Order within the array is guaranteed ifsortBy
is specified.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $push
-
addToSet
public static <TExpression> WindowOutputField addToSet(String path, TExpression expression, @Nullable Window window) Builds a window output field that adds the evaluation results of theexpression
over thewindow
to a BSONArray
and excludes duplicates. Order within the array is not specified.- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $addToSet
-
first
public static <TExpression> WindowOutputField first(String path, TExpression expression, @Nullable Window window) Builds a window output field of the evaluation result of theexpression
against the first document in thewindow
.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $first
-
firstN
public static <InExpression,NExpression> WindowOutputField firstN(String path, InExpression inExpression, NExpression nExpression, @Nullable Window window) Builds a window output field of a BSONArray
of evaluation results of theinExpression
against the firstN
documents in thewindow
, whereN
is the positive integral value of thenExpression
.Sorting is required.
- Type Parameters:
InExpression
- The type of the input expression.NExpression
- The type of the limiting expression.- Parameters:
path
- The output field path.inExpression
- The input expression.nExpression
- The expression limiting the number of produced values.window
- The window.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
-
top
public static <OutExpression> WindowOutputField top(String path, Bson sortBy, OutExpression outExpression, @Nullable Window window) Builds a window output field of the evaluation result of theoutExpression
against the top document in thewindow
sorted according to the providedsortBy
specification.- Type Parameters:
OutExpression
- The type of the output expression.- Parameters:
path
- The output field path.sortBy
- The sortBy specification. The syntax is identical to the one expected byAggregates.sort(Bson)
.outExpression
- The input expression.window
- The window.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
-
topN
public static <OutExpression,NExpression> WindowOutputField topN(String path, Bson sortBy, OutExpression outExpression, NExpression nExpression, @Nullable Window window) Builds a window output field of a BSONArray
of evaluation results of theoutExpression
against the topN
documents in thewindow
sorted according to the providedsortBy
specification, whereN
is the positive integral value of thenExpression
.- Type Parameters:
OutExpression
- The type of the output expression.NExpression
- The type of the limiting expression.- Parameters:
path
- The output field path.sortBy
- The sortBy specification. The syntax is identical to the one expected byAggregates.sort(Bson)
.outExpression
- The input expression.nExpression
- The expression limiting the number of produced values.window
- The window.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
-
last
public static <TExpression> WindowOutputField last(String path, TExpression expression, @Nullable Window window) Builds a window output field of the evaluation result of theexpression
against the last document in thewindow
.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.window
- The window.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $last
-
lastN
public static <InExpression,NExpression> WindowOutputField lastN(String path, InExpression inExpression, NExpression nExpression, @Nullable Window window) Builds a window output field of a BSONArray
of evaluation results of theinExpression
against the lastN
documents in thewindow
, whereN
is the positive integral value of thenExpression
.Sorting is required.
- Type Parameters:
InExpression
- The type of the input expression.NExpression
- The type of the limiting expression.- Parameters:
path
- The output field path.inExpression
- The input expression.nExpression
- The expression limiting the number of produced values.window
- The window.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
-
bottom
public static <OutExpression> WindowOutputField bottom(String path, Bson sortBy, OutExpression outExpression, @Nullable Window window) Builds a window output field of the evaluation result of theoutExpression
against the bottom document in thewindow
sorted according to the providedsortBy
specification.- Type Parameters:
OutExpression
- The type of the output expression.- Parameters:
path
- The output field path.sortBy
- The sortBy specification. The syntax is identical to the one expected byAggregates.sort(Bson)
.outExpression
- The input expression.window
- The window.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
-
bottomN
public static <OutExpression,NExpression> WindowOutputField bottomN(String path, Bson sortBy, OutExpression outExpression, NExpression nExpression, @Nullable Window window) Builds a window output field of a BSONArray
of evaluation results of theoutExpression
against the bottomN
documents in thewindow
sorted according to the providedsortBy
specification, whereN
is the positive integral value of thenExpression
.- Type Parameters:
OutExpression
- The type of the output expression.NExpression
- The type of the limiting expression.- Parameters:
path
- The output field path.sortBy
- The sortBy specification. The syntax is identical to the one expected byAggregates.sort(Bson)
.outExpression
- The input expression.nExpression
- The expression limiting the number of produced values.window
- The window.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
-
shift
public static <TExpression> WindowOutputField shift(String path, TExpression expression, @Nullable TExpression defaultExpression, int by) Builds a window output field of the evaluation result of theexpression
for the document whose position is shifted by the given amount relative to the current document. If the shifted document is outside of the partition containing the current document, then thedefaultExpression
is used instead of theexpression
.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.defaultExpression
- The default expression. Ifnull
, then the default expression is evaluated to BSONnull
. Must evaluate to a constant value.by
- The shift specified similarly to rules for window bounds:- 0 means the current document;
- a negative value refers to the document preceding the current one;
- a positive value refers to the document following the current one.
- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $shift
-
documentNumber
Builds a window output field of the order number of each document in its partition.Sorting is required.
- Parameters:
path
- The output field path.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $documentNumber
-
rank
Builds a window output field of the rank of each document in its partition. Documents with the same value(s) of the sortBy fields result in the same ranking and result in gaps in the returned ranks. For example, a partition with the sequence [1, 3, 3, 5] representing the values of the singlesortBy
field produces the following sequence of rank values: [1, 2, 2, 4].Sorting is required.
- Parameters:
path
- The output field path.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $rank
-
denseRank
Builds a window output field of the dense rank of each document in its partition. Documents with the same value(s) of the sortBy fields result in the same ranking but do not result in gaps in the returned ranks. For example, a partition with the sequence [1, 3, 3, 5] representing the values of the singlesortBy
field produces the following sequence of rank values: [1, 2, 2, 3].Sorting is required.
- Parameters:
path
- The output field path.- Returns:
- The constructed windowed output field.
- MongoDB documentation
- $denseRank
-
locf
Builds a window output field of the last observed non-Null
evaluation result of theexpression
. -
linearFill
Builds a window output field of a value that is equal to the evaluation result of theexpression
when it is non-Null
, or to the linear interpolation of surrounding evaluation results of theexpression
when the result isNull
.Sorting is required.
- Type Parameters:
TExpression
- The expression type.- Parameters:
path
- The output field path.expression
- The expression.- Returns:
- The constructed windowed output field.
- Since:
- 4.7
- MongoDB documentation
- $linearFill
- Since server release
- 5.3
-