提供以下格式的文件集
[
{
"status": "RESOLVED",
"transactionId": "123abc",
"associatedId": "association-1",
"timestamp": "2022-01-01"
},
{
"status": "RESOLVED",
"transactionId": "123abc",
"associatedId": "association-1",
"timestamp": "2022-01-02"
},
{
"status": "NOT_RESOLVED",
"transactionId": "456xyz",
"associatedId": "association-1",
"timestamp": "2022-01-02"
},
{
"status": "RESOLVED",
"transactionId": "456xyz",
"associatedId": "association-1",
"timestamp": "2022-01-02"
},
{
"status": "RESOLVED",
"transactionId": "456xyz",
"associatedId": "association-1",
"timestamp": "2022-01-01"
},
{
"transactionId": "456xyz",
"associatedId": "association-1",
"timestamp": "2022-01-01"
},
{
"status": "NOT_RESOLVED",
"transactionId": "456xyz",
"associatedId": "association-2",
"timestamp": "2022-01-02"
},
{
"status": "RESOLVED",
"transactionId": "456xyz",
"associatedId": "association-2",
"timestamp": "2022-01-02"
},
{
"transactionId": "456xyz",
"associatedId": "association-2",
"timestamp": "2022-01-01"
}
]
尝试根据每个associatedId查找每个transactionId的状态总数,而不考虑时间戳和总计
所以我输出应该是
| associatedId | transactionId | RESOLVED_TOTAL | NOT_RESOLVED_TOTAL | ABSENT_TOTAL (i.e status field is absent in the document)| GRAND_TOTAL |
| association-1 | "123abc" | 2 | 0 | 0 | 2 |
| association-1 | "456xyz" | 2 | 1 | 1 | 4 |
| association-2 | "456xyz" | 1 | 1 | 1 | 3 |
我尝试首先使用$project
并设置状态:'ABSENT',其中status
字段对于所有文档都不存在。然后,首先基于associatedId
、transactionId
和status
使用$group
,然后使用第二个$group
创建一个数组,该数组包含associatedId
和status
的total
,但是我被卡住了。不确定是否能继续。非常感谢你的指点。
1条答案
按热度按时间nnt7mjpx1#
使用
$ifNull
捕获缺失情况(即字段缺失或字段:空值)。Mongo Playground