Skip to content

Commit bbf28d2

Browse files
DOCSP-10258 window functions
1 parent 7873374 commit bbf28d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4356
-226
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
- Ignores non-numeric values, ``null`` values, and missing fields in a
2+
window.
3+
4+
- If the window contains one document, returns ``null``.
5+
(Compare to |covariance-method|, which returns |covariance-value| if
6+
the window contains one document.)
7+
8+
- If the window is empty, returns ``null``.
9+
10+
- If the window contains a ``NaN`` value, returns ``NaN``.
11+
12+
- If the window contains one or more ``Infinity`` value(s) that are
13+
all positive or all negative, returns ``Infinity``. The returned
14+
``Infinity`` value has the same sign as the ``Infinity`` values in the
15+
window.
16+
17+
- If the window contains ``Infinity`` values with different signs,
18+
returns ``NaN``.
19+
20+
- If the window contains a ``decimal`` value, returns a ``decimal``
21+
value.
22+
23+
- If none of the previous points apply, returns a ``double`` value.
24+
25+
The returned values in order of precedence are as follows:
26+
27+
- ``NaN``
28+
29+
- ``Infinity``
30+
31+
- ``decimal``
32+
33+
- ``double``
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
You must specify either :ref:`N <expMovingAvg-N>` or :ref:`alpha
2+
<expMovingAvg-alpha>`. You cannot specify both.

source/includes/extracts-agg-operators.yaml

Lines changed: 253 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,8 @@ content: |
930930
ref: agg-operators-accumulators-group
931931
content: |
932932
933+
.. versionchanged:: 5.0
934+
933935
.. list-table::
934936
:header-rows: 1
935937
:widths: 20 80
@@ -943,36 +945,62 @@ content: |
943945
944946
* - :group:`$addToSet`
945947
946-
- Returns an array of *unique* expression values for each
947-
group. Order of the array elements is undefined.
948+
- Returns an array of *unique* expression values for each group.
949+
Order of the array elements is undefined.
950+
951+
.. versionchanged:: 5.0
952+
953+
Available in :pipeline:`$setWindowFields` stage.
948954
949955
* - :group:`$avg`
950956
951957
- Returns an average of numerical values. Ignores non-numeric values.
952958
959+
.. versionchanged:: 5.0
960+
961+
Available in :pipeline:`$setWindowFields` stage.
962+
953963
* - :group:`$count`
954964
955-
- Returns the count of the documents in the stage. May be used in
956-
place of ``{ $sum : 1 }``.
965+
- Returns the number of documents in a group.
966+
967+
Distinct from the :pipeline:`$count` pipeline stage.
968+
969+
.. versionadded:: 5.0
970+
971+
Available in :pipeline:`$group` and
972+
:pipeline:`$setWindowFields` stages.
957973
958974
* - :group:`$first`
959975
960976
- Returns a value from the first document for each group. Order
961-
is only defined if the documents are in a defined order.
977+
is only defined if the documents are sorted.
962978
963979
Distinct from the :expression:`$first` array operator.
964980
981+
.. versionchanged:: 5.0
982+
983+
Available in :pipeline:`$setWindowFields` stage.
984+
965985
* - :group:`$last`
966986
967987
- Returns a value from the last document for each group. Order
968-
is only defined if the documents are in a defined order.
988+
is only defined if the documents are sorted.
969989
970990
Distinct from the :expression:`$last` array operator.
971991
992+
.. versionchanged:: 5.0
993+
994+
Available in :pipeline:`$setWindowFields` stage.
995+
972996
* - :group:`$max`
973997
974998
- Returns the highest expression value for each group.
975999
1000+
.. versionchanged:: 5.0
1001+
1002+
Available in :pipeline:`$setWindowFields` stage.
1003+
9761004
* - :expression:`$mergeObjects`
9771005
9781006
- Returns a document created by combining the input documents
@@ -982,32 +1010,52 @@ content: |
9821010
9831011
- Returns the lowest expression value for each group.
9841012
1013+
.. versionchanged:: 5.0
1014+
1015+
Available in :pipeline:`$setWindowFields` stage.
1016+
9851017
* - :group:`$push`
9861018
987-
- Returns an array of expression values for each group.
1019+
- Returns an array of expression values for documents in each
1020+
group.
1021+
1022+
.. versionchanged:: 5.0
9881023
1024+
Available in :pipeline:`$setWindowFields` stage.
9891025
9901026
* - :group:`$stdDevPop`
9911027
9921028
- Returns the population standard deviation of the input values.
9931029
1030+
.. versionchanged:: 5.0
1031+
1032+
Available in :pipeline:`$setWindowFields` stage.
1033+
9941034
* - :group:`$stdDevSamp`
9951035
9961036
- Returns the sample standard deviation of the input values.
9971037
1038+
.. versionchanged:: 5.0
1039+
1040+
Available in :pipeline:`$setWindowFields` stage.
1041+
9981042
* - :group:`$sum`
9991043
10001044
- Returns a sum of numerical values. Ignores non-numeric values.
10011045
1046+
.. versionchanged:: 5.0
1047+
1048+
Available in :pipeline:`$setWindowFields` stage.
10021049
10031050
---
10041051
ref: agg-operators-accumulators-project
10051052
content: |
10061053
1007-
.. versionchanged:: 3.2
1054+
.. versionchanged:: 5.0
10081055
10091056
The following accumulator operators are also available in the
1010-
:pipeline:`$project`, :pipeline:`$addFields`, and :pipeline:`$set` stages.
1057+
:pipeline:`$project`, :pipeline:`$addFields`, :pipeline:`$set`, and,
1058+
starting in MongoDB 5.0, the :pipeline:`$setWindowFields` stages.
10111059
10121060
.. list-table::
10131061
:header-rows: 1
@@ -1021,7 +1069,6 @@ content: |
10211069
- Returns an average of the specified expression or list of
10221070
expressions for each document. Ignores non-numeric values.
10231071
1024-
10251072
* - :group:`$max`
10261073
10271074
- Returns the maximum of the specified expression or list of
@@ -1044,4 +1091,200 @@ content: |
10441091
10451092
- Returns a sum of numerical values. Ignores non-numeric values.
10461093
1094+
---
1095+
ref: agg-operators-window
1096+
content: |
1097+
1098+
.. versionadded:: 5.0
1099+
1100+
Window operators return values from a defined span of documents from
1101+
a collection, known as a *window*. A :ref:`window
1102+
<setWindowFields-window>` is defined in the
1103+
:pipeline:`$setWindowFields` stage, available starting in MongoDB
1104+
5.0.
1105+
1106+
The following window operators are available in the
1107+
:pipeline:`$setWindowFields` stage.
1108+
1109+
.. list-table::
1110+
:header-rows: 1
1111+
:widths: 20 80
1112+
1113+
* - Name
1114+
- Description
1115+
1116+
* - :group:`$addToSet`
1117+
1118+
- Returns an array of all unique values that results from
1119+
applying an :ref:`expression <aggregation-expressions>` to
1120+
each document.
1121+
1122+
.. versionchanged:: 5.0
1123+
1124+
Available in :pipeline:`$setWindowFields` stage.
1125+
1126+
* - :group:`$avg`
1127+
1128+
- Returns the average for the specified :ref:`expression
1129+
<aggregation-expressions>`. Ignores non-numeric values.
1130+
1131+
.. versionchanged:: 5.0
1132+
1133+
Available in :pipeline:`$setWindowFields` stage.
1134+
1135+
* - :group:`$count`
1136+
1137+
- Returns the number of documents in the group or window.
1138+
1139+
Distinct from the :pipeline:`$count` pipeline stage.
1140+
1141+
.. versionadded:: 5.0
1142+
1143+
* - :group:`$covariancePop`
1144+
1145+
- Returns the population covariance of two numeric
1146+
:ref:`expressions <aggregation-expressions>`.
1147+
1148+
.. versionadded:: 5.0
1149+
1150+
* - :group:`$covarianceSamp`
1151+
1152+
- Returns the sample covariance of two numeric
1153+
:ref:`expressions <aggregation-expressions>`.
1154+
1155+
.. versionadded:: 5.0
1156+
1157+
* - :group:`$denseRank`
1158+
1159+
- Returns the document position (known as the rank) relative to
1160+
other documents in the :pipeline:`$setWindowFields` stage
1161+
:ref:`partition <setWindowFields-partitionBy>`. There are no
1162+
gaps in the ranks. Ties receive the same rank.
1163+
1164+
.. versionadded:: 5.0
1165+
1166+
* - :group:`$derivative`
1167+
1168+
- Returns the mathematical derivative of the numeric
1169+
:ref:`expression <aggregation-expressions>`.
1170+
1171+
.. versionadded:: 5.0
1172+
1173+
* - :group:`$documentNumber`
1174+
1175+
- Returns the position of a document (known as the document
1176+
number) in the :pipeline:`$setWindowFields` stage
1177+
:ref:`partition <setWindowFields-partitionBy>`. Ties result in
1178+
different adjacent document numbers.
1179+
1180+
.. versionadded:: 5.0
1181+
1182+
* - :group:`$expMovingAvg`
1183+
1184+
- Returns the exponential moving average for the numeric
1185+
:ref:`expression <aggregation-expressions>`.
1186+
1187+
.. versionadded:: 5.0
1188+
1189+
* - :group:`$first`
1190+
1191+
- Returns the value that results from applying an
1192+
:ref:`expression <aggregation-expressions>` to the first
1193+
document in a group or :ref:`window <setWindowFields-window>`.
1194+
1195+
.. versionchanged:: 5.0
1196+
1197+
Available in :pipeline:`$setWindowFields` stage.
1198+
1199+
* - :group:`$integral`
1200+
1201+
- Returns the mathematical integral of the numeric
1202+
:ref:`expression <aggregation-expressions>`.
1203+
1204+
.. versionadded:: 5.0
1205+
1206+
* - :group:`$last`
1207+
1208+
- Returns the value that results from applying an
1209+
:ref:`expression <aggregation-expressions>` to the last
1210+
document in a group or :ref:`window <setWindowFields-window>`.
1211+
1212+
.. versionchanged:: 5.0
1213+
1214+
Available in :pipeline:`$setWindowFields` stage.
1215+
1216+
* - :group:`$max`
1217+
1218+
- Returns the maximum value that results from applying an
1219+
:ref:`expression <aggregation-expressions>` to each document.
1220+
1221+
.. versionchanged:: 5.0
1222+
1223+
Available in :pipeline:`$setWindowFields` stage.
1224+
1225+
* - :group:`$min`
1226+
1227+
- Returns the minimum value that results from applying an
1228+
:ref:`expression <aggregation-expressions>` to each document.
1229+
1230+
.. versionchanged:: 5.0
1231+
1232+
Available in :pipeline:`$setWindowFields` stage.
1233+
1234+
* - :group:`$push`
1235+
1236+
- Returns an array of values that result from applying an
1237+
:ref:`expression <aggregation-expressions>` to each document.
1238+
1239+
.. versionchanged:: 5.0
1240+
1241+
Available in :pipeline:`$setWindowFields` stage.
1242+
1243+
* - :group:`$rank`
1244+
1245+
- Returns the document position (known as the rank) relative to
1246+
other documents in the :pipeline:`$setWindowFields` stage
1247+
:ref:`partition <setWindowFields-partitionBy>`.
1248+
1249+
.. versionadded:: 5.0
1250+
1251+
* - :group:`$shift`
1252+
1253+
- Returns the value from an :ref:`expression
1254+
<aggregation-expressions>` applied to a document in a
1255+
specified position relative to the current document in the
1256+
:pipeline:`$setWindowFields` stage :ref:`partition
1257+
<setWindowFields-partitionBy>`.
1258+
1259+
.. versionadded:: 5.0
1260+
1261+
* - :group:`$stdDevPop`
1262+
1263+
- Returns the population standard deviation that results from
1264+
applying a numeric :ref:`expression <aggregation-expressions>`
1265+
to each document.
1266+
1267+
.. versionchanged:: 5.0
1268+
1269+
Available in :pipeline:`$setWindowFields` stage.
1270+
1271+
* - :group:`$stdDevSamp`
1272+
1273+
- Returns the sample standard deviation that results from
1274+
applying a numeric :ref:`expression <aggregation-expressions>`
1275+
to each document.
1276+
1277+
.. versionchanged:: 5.0
1278+
1279+
Available in :pipeline:`$setWindowFields` stage.
1280+
1281+
* - :group:`$sum`
1282+
1283+
- Returns the sum that results from applying a numeric
1284+
:ref:`expression <aggregation-expressions>` to each document.
1285+
1286+
.. versionchanged:: 5.0
1287+
1288+
Available in :pipeline:`$setWindowFields` stage.
1289+
10471290
...

0 commit comments

Comments
 (0)