如何使用 mongodb 聚合查找嵌套数组内的值的总和? [英] How to find the sum of values that are inside a nested array using mongodb aggregation?
问题描述
我正在使用 node.js 并使用 Express 框架编写脚本.我正在使用云 Mongodb 并且无法访问 mongo shell,因此我无法轻松检查输出.我必须使用 toArrray
函数将结果放入一个数组中,这样我才能看到输出的内容.Console.log 有帮助,但有时当它是一个对象时它只显示 []
,尽管如果我检查 Postman,我可以看到对象中的属性.
I am using node.js and writing a script using the Express framework. I am using cloud Mongodb and don't have access to a mongo shell, so I can't check output easily. I have to put the result in an array, using the toArrray
function, so that I can see what's being put out. Console.log helps but sometimes it shows just []
when it is an object, although I can see the properties within the object if I check Postman.
我有一个文档,我一直在向 mongodb 中的聚合管道发送文档,此时我卡住了,res.send
中出现的输出是这样的:
I have a document that I have been sending down an aggregation pipeline in mongodb and at this point, where I am stuck, the output that appears in res.send
is this:
[ { japanese_array: [ {"k": "japanese3a", "v": { "question": "japanese3a", "value": 100}},
{"k": "japanese3b", "v": { "question": "japanese3b", "value": 100}},
{"k": "japanese3c", "v": { "question": "japanese3c", "value": 100}} ] } ]
我想添加名为值"的字段,因此所需的输出只是一个数字 (300).
I want to add the fields called "value", so the desired output is just a number (300).
到达这一点需要很长时间,因为遵循 Stackoverflow 中的解决方案示例有很多麻烦,因为它们似乎使用了很多引号(例如$project"),如果我复制我的脚本中的示例,包括引号,脚本不起作用.我必须删除 $project 等周围的引号才能使事情正常工作.我不明白为什么在某些词周围加上引号对其他人有用而对我不起作用.我使用的是 mongodb 4.2 版.
It's taken a long time to arrive at this point as there was a lot of trouble following examples of solutions in Stackoverflow as they seem to use a lot of quotes (eg. "$project"), and if I copy the examples in my script, with the quotes included, the script does not work. I have to remove the quotes around $project, etc to get the thing working. I don't understand why putting quotes around certain words work for other people and it doesn't for me. I am using mongodb version 4.2.
我已经在 stackoverflow 中检查了其他类似的问题好几天了,但没有一个解决方案适合我.
I have checked other similar questions in stackoverflow for days, but none of the solutions work for me.
最接近我回答的一个来自这里:如何对MongoDB的子文档中的每个字段求和?
The one that was closest to answering it for me is from here: How to sum every fields in a sub document of MongoDB?
但是还是不行.我曾尝试在 $group
表达式中求和 "$japanese_array.v.value"
但它不起作用,我被困在此处显示的点上.>
But it still does not work. I have tried summing "$japanese_array.v.value"
in a $group
expression but it's not working, and I am stuck at the point that's shown here.
推荐答案
我解决了这个问题.
我对管道做了这个额外的阶段:
I did this additional stage to the pipeline:
{$project: {_id:0, "result":{$sum: "$japanese_array.v.value"}}}
两天前我问了一个类似的问题,然后我得到了答案您如何使用 node.js 对作为 mongodb 中嵌套子文档的对象内的值求和? 帮助我在这里找到了解决方案.
I asked a similar question 2 days ago and the answer I got then How do you sum the values within an object which is a nested subdocument in mongodb using node.js? helped me find a solution here.
这篇关于如何使用 mongodb 聚合查找嵌套数组内的值的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!