Skip to content

Commit 9dd43ef

Browse files
committed
Add reference files
1 parent 3a2123c commit 9dd43ef

File tree

11 files changed

+2244
-0
lines changed

11 files changed

+2244
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
title: $covariancePop
3+
description: The $covariancePop operator returns the covariance of two numerical expressions
4+
type: operators
5+
category: window-operators
6+
---
7+
8+
# $covariancePop
9+
10+
The `$covariancePop` operator sorts documents on one or more fields within a partition and calculates a covariance of two numerical fields within a specified document window.
11+
12+
## Syntax
13+
14+
```javascript
15+
{
16+
$covariancePop: [ < numericalExpression1 > , < numericalExpression2 > ]
17+
}
18+
```
19+
20+
## Parameters
21+
22+
| Parameter | Description |
23+
| --- | --- |
24+
| **`numericalExpression1`** | The first numerical expression to use to calculate the covariance within the specified document window|
25+
| **`numericalExpression2`** | The first numerical expression to use to calculate the covariance within the specified document window|
26+
27+
## Examples
28+
29+
Consider this sample document from the stores collection.
30+
31+
```json
32+
{
33+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
34+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
35+
"location": {
36+
"lat": -89.2384,
37+
"lon": -46.4012
38+
},
39+
"staff": {
40+
"totalStaff": {
41+
"fullTime": 8,
42+
"partTime": 20
43+
}
44+
},
45+
"sales": {
46+
"totalSales": 75670,
47+
"salesByCategory": [
48+
{
49+
"categoryName": "Wine Accessories",
50+
"totalSales": 34440
51+
},
52+
{
53+
"categoryName": "Bitters",
54+
"totalSales": 39496
55+
},
56+
{
57+
"categoryName": "Rum",
58+
"totalSales": 1734
59+
}
60+
]
61+
},
62+
"promotionEvents": [
63+
{
64+
"eventName": "Unbeatable Bargain Bash",
65+
"promotionalDates": {
66+
"startDate": {
67+
"Year": 2024,
68+
"Month": 6,
69+
"Day": 23
70+
},
71+
"endDate": {
72+
"Year": 2024,
73+
"Month": 7,
74+
"Day": 2
75+
}
76+
},
77+
"discounts": [
78+
{
79+
"categoryName": "Whiskey",
80+
"discountPercentage": 7
81+
},
82+
{
83+
"categoryName": "Bitters",
84+
"discountPercentage": 15
85+
},
86+
{
87+
"categoryName": "Brandy",
88+
"discountPercentage": 8
89+
},
90+
{
91+
"categoryName": "Sports Drinks",
92+
"discountPercentage": 22
93+
},
94+
{
95+
"categoryName": "Vodka",
96+
"discountPercentage": 19
97+
}
98+
]
99+
},
100+
{
101+
"eventName": "Steal of a Deal Days",
102+
"promotionalDates": {
103+
"startDate": {
104+
"Year": 2024,
105+
"Month": 9,
106+
"Day": 21
107+
},
108+
"endDate": {
109+
"Year": 2024,
110+
"Month": 9,
111+
"Day": 29
112+
}
113+
},
114+
"discounts": [
115+
{
116+
"categoryName": "Organic Wine",
117+
"discountPercentage": 19
118+
},
119+
{
120+
"categoryName": "White Wine",
121+
"discountPercentage": 20
122+
},
123+
{
124+
"categoryName": "Sparkling Wine",
125+
"discountPercentage": 19
126+
},
127+
{
128+
"categoryName": "Whiskey",
129+
"discountPercentage": 17
130+
},
131+
{
132+
"categoryName": "Vodka",
133+
"discountPercentage": 23
134+
}
135+
]
136+
}
137+
]
138+
}
139+
```
140+
141+
### Example 1 - Calculate the covariance in sales volume
142+
143+
To get the covariance in total sales for stores in the First Up Consultants company, first run a query to filter on the company name, then sort the resulting documents in ascending order of the last updated timestamp, and calculate the covariance between the first and current document in the sorted result set.
144+
145+
```javascript
146+
db.stores.aggregate(
147+
[{
148+
"$match": {
149+
"company": {
150+
"$in": [
151+
"First Up Consultants"
152+
]
153+
}
154+
}
155+
},
156+
{
157+
"$setWindowFields": {
158+
"partitionBy": "$company",
159+
"sortBy": {
160+
"lastUpdated": 1
161+
},
162+
"output": {
163+
"covariancePopForSales": {
164+
"$covariancePop": [{
165+
"$hour": "$lastUpdated"
166+
},
167+
"$sales.totalSales"
168+
],
169+
"window": {
170+
"documents": [
171+
"unbounded",
172+
"current"
173+
]
174+
}
175+
}
176+
}
177+
}
178+
},
179+
{
180+
"$project": {
181+
"company": 1,
182+
"name": 1,
183+
"sales.totalSales": 1,
184+
"lastUpdated": 1,
185+
"covariancePopForSales": 1
186+
}
187+
}
188+
]
189+
)
190+
```
191+
192+
The first two results returned by this query are:
193+
194+
```json
195+
[
196+
{
197+
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
198+
"sales": {},
199+
"company": "First Up Consultants",
200+
"lastUpdated": "2025-06-11T10:48:01.291Z",
201+
"name": "First Up Consultants | Bed and Bath Center - South Amir",
202+
"covariancePopForSales": null
203+
},
204+
{
205+
"_id": "8e7a259b-f7d6-4ec5-a521-3bed53adc587",
206+
"name": "First Up Consultants | Drone Stop - Lake Joana",
207+
"sales": {},
208+
"company": "First Up Consultants",
209+
"lastUpdated": {
210+
"t": 1727827539,
211+
"i": 1
212+
},
213+
"covariancePopForSales": null
214+
}
215+
]
216+
```

0 commit comments

Comments
 (0)