我每週收到一次服務的數據,並將其放入一個集合中。數據有一個金額,projectNo和dataDate時間戳。使用聚合框架我通過projectNo和dataDate總結量和組:Mongodb聚合框架:月份中最大日期的總和值
db.collection.aggregate([
{$project: {projectNo: 1, bdgtAppd: 1, dataDate: 1}},
{$group: {_id: {
projectNo: "$projectNo",
dataDate: "$dataDate"
},
amount: {$sum: "$bdgtAppd"}}
},
{$project: {_id:0,
projectNo:"$_id.projectNo",
dataDate:"$_id.dataDate",
amount:"$amount"
}
},
{$sort: {projectNo:1,dataDate:1}}
])
其中產量如下:
[{
"amount" : 7887,
"projectNo" : "5544A",
"dataDate" : "2015-01-02T08:00:00.000Z"
}, {
"amount" : 137947,
"projectNo" : "5544A",
"dataDate" : "2015-01-16T08:00:00.000Z"
}, {
"amount" : 137947,
"projectNo" : "5544A",
"dataDate" : "2015-01-23T08:00:00.000Z"
}, {
"amount" : 137947,
"projectNo" : "5544A",
"dataDate" : "2015-01-30T08:00:00.000Z"
}, {
"amount" : 130060,
"projectNo" : "5544A",
"dataDate" : "2015-02-06T08:00:00.000Z"
}, {
"amount" : 130060,
"projectNo" : "5544A",
"dataDate" : "2015-02-13T08:00:00.000Z"
}, {
"amount" : 130060,
"projectNo" : "5544A",
"dataDate" : "2015-02-20T08:00:00.000Z"
}]
我現在需要做的是限制返回的數據只是最後日期每月:
[{
"amount" : 137947,
"projectNo" : "5544A",
"dataDate" : "2015-01-30T08:00:00.000Z"
}, {
"amount" : 130060,
"projectNo" : "5544A",
"dataDate" : "2015-02-27T08:00:00.000Z"
}]
編輯:
{
"_id" : ObjectId("5527e724fc53ec16bc5fe57a"),
"projectNo" : "5544G",
"cpfoNo" : "1448R",
"cpfoDate" : ISODate("2014-10-20T07:00:00Z"),
"description" : "INC 6 CO 176 - Booster Pump",
"pcoNo" : "1510",
"approvedAmount" : null,
"days" : null,
"remarks" : null,
"itemNo" : "0005",
"costCode" : "5030.09900.0000.0000",
"itemTitle" : "Painting - Hasson",
"bdgtEst" : 0.0,
"bdgtProp" : 745.0,
"bdgtAprv" : 745.0,
"bdgtAppd" : 745.0,
"dataDate" : ISODate("2014-12-12T08:00:00Z")
}
:從收集樣品文件
你能告訴我們你的文件嗎? – styvane
@Michael你有沒有機會看看這個? – Splitty