【发布时间】:2022-01-08 13:06:20
【问题描述】:
我的 mongodb 集合中有很多记录,我需要重新计算一些信息。
记录格式:
{
"_id" : someId,
"targetFrom" : ObjectId("603e0355e805140334e79438"),<-- this is ID for search
"targetTo" : null,
"operationPaid" : true,
"type" : "coming", <--- type
"moneyAccount" : someId,
"agent" : null,
"sum" : 5000, <--- sum
}
{
"_id" : someId,
"targetFrom" : null,
"targetTo" : null,
"operationPaid" : true,
"type" : "out", <--- type
"moneyAccount" : someId,
"agent" : ObjectId("603e0355e805140334e79438"),<-- this is ID for search
"sum" : 3000, <--- sum
}
所以,我需要按记录分组 TYPE 并为 id ObjectId("603e0355e805140334e79438") 获取 SUM,但搜索的 id 可以是字段 targetFrom 或 targetTo 或 agent
对于这个例子,我需要得到结果 2000
sum 5000 即将到来,而 sum 3000 出局
【问题讨论】:
标签: mongodb express mongoose aggregation