我有一个包含此类对象的JSON数组
{
"InvoiceNo": "FA 2019/1",
"Period": "01",
"DocumentTotals": {
"TaxPayable": "26.94",
"NetTotal": "117.16",
"GrossTotal": "144.10"
},
"WithholdingTax": {
"WithholdingTaxAmount": "0.00"
}
},
我想对各个对象的GrossTotal求和并按Period将其分组。 我尝试使用以下代码:
saftFileController.revenuePerMonth = function (req, res) {
Saft.find().exec(function (err, fileContent) {
if (err) {
console.log(err);
} else {
const JSONObject = JSON.parse(JSON.stringify(fileContent));
const sales = JSONObject[3].SalesInvoices.Invoice;
const revenuePerMonth = Saft.aggregate([
{
$group: {
Period: sales.Period,
revenue: {
$sum: "$GrossTotal",
},
},
},
]);
res.json({ revenue: revenuePerMonth });
}
});
};
但是输出不是想要的。我做错了什么?这是输出:
{
"revenue": {
"_pipeline": [
{
"$group": {
"revenue": {
"$sum": "$GrossTotal"
}
}
}
],
"options": {}
}
}
这是我数据库的一些打印屏幕
https://snipboard.io/QOfiYz.jpg
https://snipboard.io/72LSRC.jpg
执行此操作,现在返回“周期”,但是由于它是字符串类型,因此它忽略了总和。我该如何转换?
saftFileController.revenuePerMonth = function (req, res) {
Saft.aggregate([
{
$group: {
_id: "$SalesInvoices.Invoice.Period",
revenue: {
$sum: "SalesInvoices.Invoice.DocumentTotals.GrossTotal",
},
},
},
]).exec(function (err, fileContent) {
if (err) {
console.log("Error: ", error);
} else {
res.json({ revenuePerMonth: fileContent });
}
});
};
答案 0 :(得分:0)
namespace = {'v': "urn:schemas-microsoft-com:vml"}
results = ET.fromstring(xml).findall("imagedata", namespace)
for image_id in results:
print(image_id)
阶段(documentation)中传递要作为_id
分组的MongoDB表达式。 $ sum表达式也应包含完整路径。$group
$group: {
_id: '$Period',
revenue: {
$sum: "$DocumentTotals.GrossTotal",
},
},
,就像您使用Promise
Saft.find()