【发布时间】:2016-07-01 09:47:34
【问题描述】:
我在 mongoDB 中有一组数据,我必须将它们按$timestamp 分组。此字段包含日期,但格式为字符串(上面的示例数据)。
我应该如何继续将$timestamp 转换为日期,以便将它们组合在一起?
接下来,我必须为每个日期和标识对每个 scores_today 求和,并且对每个 scores_total 求和。
示例数据:
[
{
_id: "1442",
timestamp: "2016-03-15T22:24:02.000Z",
iden: "15",
scores_today: "0.000000",
scores_total: "52337.000000"
}
]
我的代码
var project = {
"$project":{
"_id": 0,
"y": {
"$year": "$timestamp" // tried this way, not working
},
"m": {
"$month": new Date("$timestamp") // tried either this, not working
},
"d": {
"$dayOfMonth": new Date("$timestamp")
},
"iden" : "$iden"
}
},
group = {
"$group": {
"_id": {
"iden" : "$iden",
"year": "$y",
"month": "$m",
"day": "$d"
},
"count" : { "$sum" : "$scores_today" }
}
};
mongoDB.collection('raw').aggregate([ project, group ]).toArray()....
这是 node.js 服务记录的错误
Err: { [MongoError: exception: can't convert from BSON type String to Date] name: 'MongoError', message: 'exception: can\'t convert from BSON type String to Date', errmsg: 'exception: can\'t convert from BSON type String to Date', code: 16006, ok: 0 }
【问题讨论】:
标签: mongodb aggregation-framework