【发布时间】:2017-01-09 16:37:21
【问题描述】:
[
{
"rating": 4,
"createdAt": ISODate("2016-08-08T15:32:41.262+0000")
},
{
"rating": 4,
"createdAt": ISODate("2016-08-08T15:32:41.262+0000")
},
{
"rating": 3,
"createdAt": ISODate("2016-07-01T15:32:41.262+0000")
},
{
"rating": 5,
"createdAt": ISODate("2016-07-01T15:32:41.262+0000")
}
]
这是我的数据集。
我用它来获取月份数。
[
{
"$project": {
"createdAtMonth": { "$month": "$createdAt" },
"rating": 1
}
},
{
"$group": {
"_id": "$createdAtMonth",
"average": { "$avg": "$rating" },
"month": { "$first": "$createdAtMonth" }
}
}
]
我需要在时间戳中获取该月的开始日期。我该怎么做?
预期输出:
[{
"_id": 8,
"average": 4,
"month": 8,
"time": 1469989800000
}, {
"_id": 7,
"average": 4,
"month": 7,
"time": 1467311400000
}]
此输出中的日期可能不正确。但根据输入,我应该得到两个对象,一个是 7 月,一个是 8 月
【问题讨论】:
-
编辑问题
标签: mongodb date timestamp mongodb-query aggregation-framework