我使用Node.JS和Express创建了一个服务器。数据库是MongoDB。 这就是我要做的。
根据版本字段和到期日期(即3月3日)的当前月份,应相对于“ curr”字段添加“ value”字段中的所有int值。
这是我收藏中的对象数据。
{
"_id" : ObjectId("5c98d1276e20472820b2e924"),
"curr" : "INR",
"version" : "K3",
"value" : 30,
"expiry" : "2019-03-25T13:01:26.732Z"
},
{
"_id" : ObjectId("5c98d12b6e20472820b2e925"),
"curr" : "INR",
"version" : "K4",
"value" : 10,
"expiry" : "2019-03-25T13:01:30.489Z"
},
{
"_id" : ObjectId("5c98d12c6e20472820b2e926"),
"curr" : "USD",
"version" : "K3",
"value" : 15,
"expiry" : "2019-02-21T13:01:31.500Z"
},
{
"_id" : ObjectId("5c98d12d6e20472820b2e927"),
"curr" : "EUR",
"version" : "K3",
"value" : 3,
"expiry" : "2019-01-17T13:01:32.517Z"
},
{
"_id" : ObjectId("5c98d1fac021c738a4d14130"),
"curr" : "INR",
"version" : "K3",
"value" : 10,
"expiry" : "2019-03-25T13:01:30.489Z"
}
这是我在nodejs中尝试过的
dbo.collection(collectionName).aggregate(
[
{
$match : { version : version, month: { $month: "$expiry" }}
},
{
$group : {
_id : {
currency : "$curr",
version:"$version",
totalValue:{$sum : "$value"}
},
}
}
]
).toArray(function(errr,docss){
if(!errr){
if(docss.length>0){
res.end(JSON.stringify(docss));
}
else{
console.log(`No data found for ${version}.`);
res.end(JSON.stringify([]));
}
}
else{
console.log(errr);
}
});
结果必须是这样
[{"_id":{"currency":"INR","version":"K3","month":3,"totalValue":40}},{"_id":{"currency":"EUR","version":"K3","month":1,"totalValue":3}},{"_id":{"currency":"USD","version":"K3","month":2,"totalValue":15}}]
我要去哪里错了?请帮助我。