Javascript 在Mongo视图中求和并输出总计
在我的mongDB后端中,我有一个视图,在多个聚合阶段之后,输出如下所示的信息:Javascript 在Mongo视图中求和并输出总计,javascript,mongodb,aggregation-framework,Javascript,Mongodb,Aggregation Framework,在我的mongDB后端中,我有一个视图,在多个聚合阶段之后,输出如下所示的信息: { "_id" : 25k3ejfjyi32132f9z3, "customer_id" : 15cgrd582950jj493g5, "openBalance": 24, // other data... }, { "_id" : 35g6ejfjfj32132f8s4, "customer_id" : 23gtrd684563jj494f4 "ope
{
"_id" : 25k3ejfjyi32132f9z3,
"customer_id" : 15cgrd582950jj493g5,
"openBalance": 24,
// other data...
},
{
"_id" : 35g6ejfjfj32132f8s4,
"customer_id" : 23gtrd684563jj494f4
"openBalance": 20,
// other data...
}
{
"_id" : 25k3ejfjyi32132f9z3,
"customer_id" : 15cgrd582950jj493g5,
"openBalance": 24,
// other data...
},
{
"_id" : 35g6ejfjfj32132f8s4,
"customer_id" : 23gtrd684563jj494f4
"openBalance": 20,
// other data...
},
"totalOpenBalance": 44
作为最后一步,我需要做的是合计所有记录的所有“openBalance”金额,并将该数字和其他数据一起输出到一个新字段中。因此,换句话说,基于上述数据,我想在标题为totalOpenBalance
的a字段中返回44
有没有办法在mongo视图中处理此聚合逻辑?我不知道该怎么做,因为我不想为返回的每个记录添加一个字段,而是基于记录总数返回一个值?它看起来像这样:
{
"_id" : 25k3ejfjyi32132f9z3,
"customer_id" : 15cgrd582950jj493g5,
"openBalance": 24,
// other data...
},
{
"_id" : 35g6ejfjfj32132f8s4,
"customer_id" : 23gtrd684563jj494f4
"openBalance": 20,
// other data...
}
{
"_id" : 25k3ejfjyi32132f9z3,
"customer_id" : 15cgrd582950jj493g5,
"openBalance": 24,
// other data...
},
{
"_id" : 35g6ejfjfj32132f8s4,
"customer_id" : 23gtrd684563jj494f4
"openBalance": 20,
// other data...
},
"totalOpenBalance": 44
如果将以下代码添加到管道的末尾
$group: {
_id: null, // do not really group but throw all documents into the same bucket
documents: { $push: "$$ROOT" }, // push each encountered document into the group
totalOpenBalance: { $sum: "$openBalance" } // sum up all "openBalance" values
}
您将得到一些您可能能够使用的东西:
{
"_id" : null,
"documents" : [
{
"_id" : 25k3ejfjyi32132f9z3,
"customer_id" : 15cgrd582950jj493g5,
"openBalance" : 24
},
{
"_id" : 35g6ejfjfj32132f8s4,
"customer_id" : 23gtrd684563jj494f4,
"openBalance" : 20
}
],
"totalOpenBalance" : 44
}
如果你想彻底疯掉,我不推荐你这么做,那就继续读下去。通过添加以下阶段
{
$group: {
_id: null, // do not really group but throw all documents into the same bucket
documents: { $push: "$$ROOT" }, // push each encountered document into the group
totalOpenBalance: { $sum: "$openBalance" } // sum up all "openBalance" values
}
}, {
$project: {
"_id": 0, // remove the "_id" field
"documents": { $concatArrays: [ "$documents", [ { "totalOpenBalance": "$totalOpenBalance" } ] ] } // append a magic subdocument to the the existing documents
}
}, {
$unwind: "$documents" // just so we can flatten the resulting array into separate documents
}, {
$replaceRoot: {
newRoot: "$documents" // and move the content of our documents field to the root
}
}
你得到的正是你想要的:
{
"_id" : 25k3ejfjyi32132f9z3,
"customer_id" : 15cgrd582950jj493g5,
"openBalance" : 24
},
{
"_id" : 35g6ejfjfj32132f8s4,
"customer_id" : 23gtrd684563jj494f4,
"openBalance" : 20
},
{
"totalOpenBalance" : 44
}
但是,如果您在管道的末尾添加以下代码,这可能只是一种过激行为…
$group: {
_id: null, // do not really group but throw all documents into the same bucket
documents: { $push: "$$ROOT" }, // push each encountered document into the group
totalOpenBalance: { $sum: "$openBalance" } // sum up all "openBalance" values
}
您将得到一些您可能能够使用的东西:
{
"_id" : null,
"documents" : [
{
"_id" : 25k3ejfjyi32132f9z3,
"customer_id" : 15cgrd582950jj493g5,
"openBalance" : 24
},
{
"_id" : 35g6ejfjfj32132f8s4,
"customer_id" : 23gtrd684563jj494f4,
"openBalance" : 20
}
],
"totalOpenBalance" : 44
}
如果你想彻底疯掉,我不推荐你这么做,那就继续读下去。通过添加以下阶段
{
$group: {
_id: null, // do not really group but throw all documents into the same bucket
documents: { $push: "$$ROOT" }, // push each encountered document into the group
totalOpenBalance: { $sum: "$openBalance" } // sum up all "openBalance" values
}
}, {
$project: {
"_id": 0, // remove the "_id" field
"documents": { $concatArrays: [ "$documents", [ { "totalOpenBalance": "$totalOpenBalance" } ] ] } // append a magic subdocument to the the existing documents
}
}, {
$unwind: "$documents" // just so we can flatten the resulting array into separate documents
}, {
$replaceRoot: {
newRoot: "$documents" // and move the content of our documents field to the root
}
}
你得到的正是你想要的:
{
"_id" : 25k3ejfjyi32132f9z3,
"customer_id" : 15cgrd582950jj493g5,
"openBalance" : 24
},
{
"_id" : 35g6ejfjfj32132f8s4,
"customer_id" : 23gtrd684563jj494f4,
"openBalance" : 20
},
{
"totalOpenBalance" : 44
}
然而,这可能是一种过激的行为……我在mongo聚合查询中也面临类似的问题。抱歉,使用聚合管道无法做到这一点。相反,您必须在聚合管道的回调中执行此操作。基本上,您必须手动计算该“字段”,并将其添加到响应对象中。我将对此进行调查,谢谢。我的mongo聚合查询也面临类似的问题。抱歉,使用聚合管道无法做到这一点。相反,您必须在聚合管道的回调中执行此操作。基本上,您必须手动计算该“字段”,并将其添加到响应对象中。我会查清楚的,谢谢。这看起来是个好办法。谢谢这个解决方案对我也有帮助!我无法告诉你我搜索这个有多久了,但没有结果。这看起来是一条路要走。谢谢这个解决方案对我也有帮助!我无法告诉你我搜索这个有多久了,但没有结果。