$covarianceSamp 运算符根据分区内的一个或多个字段对文档进行排序,并计算指定文档窗口内样本两个数值字段的协方差。
Syntax
{
$covarianceSamp: [ < numericalExpression1 > , < numericalExpression2 > ]
}
参数
| 参数 | Description |
|---|---|
numericalExpression1 |
用于计算指定文档窗口内的协方差样本的第一个数值表达式 |
numericalExpression2 |
用于计算指定文档窗口内的协方差样本的第一个数值表达式 |
例子
请考虑商店集合中的这个示例文档。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
示例 1 - 计算从无界起始文档到当前文档之间的协方差样本
要计算 First Up Consultants 公司各商店的协方差样本,首先运行查询来筛选该公司,然后按开业日期的升序对结果商店进行排序,并计算排序结果集的销售额的协方差。
db.stores.aggregate(
[{
"$match": {
"company": {
"$in": [
"First Up Consultants"
]
},
"$and": [
{
"storeOpeningDate": {
"$gt": ISODate("2024-09-01T03:06:24.180Z")
}
},
{
"storeOpeningDate": {
"$lt": ISODate("2025-09-30T03:55:17.557Z")
}
}
]
}
},
{
"$setWindowFields": {
"partitionBy": "$company",
"sortBy": {
"storeOpeningDate": 1
},
"output": {
"covarianceSampForSales": {
"$covarianceSamp": [
{
"$hour": "$storeOpeningDate"
},
"$sales.revenue"
],
"window": {
"documents": [
"unbounded",
"current"
]
}
}
}
}
},
{
"$project": {
"company": 1,
"name": 1,
"sales.revenue": 1,
"storeOpeningDate": 1,
"covarianceSampForSales": 1
}
}]
)
此查询返回的前两个结果为:
[
{
"_id": "2d315043-db26-4d18-8bb7-71ba922f00a0",
"name": "First Up Consultants | Furniture Shoppe - Wymantown",
"sales": {
"revenue": 38042
},
"company": "First Up Consultants",
"storeOpeningDate": "2024-09-02T02:00:52.592Z",
"covarianceSampForSales": 935.0972222222222
},
{
"_id": "416adb8c-7d65-40e5-af88-8659c71194ce",
"name": "First Up Consultants | Picture Frame Bazaar - South Lysanneborough",
"sales": {
"revenue": 37157
},
"company": "First Up Consultants",
"storeOpeningDate": "2024-09-02T02:39:50.269Z",
"covarianceSampForSales": 1901.1777777777777
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。