总计$ project中的新Date() [英] new Date() in aggregate $project
问题描述
对于我的其中一个必须保持unix时间戳而不是isodate的集合,我通常将时间戳转换为新的Date(unix_timestamp).
For one of my collections, which must remain unix timestamp instead of isodate, I usually convert the timestamp with new Date(unix_timestamp).
现在我需要聚合中的new Date(ts)
. (该示例在PHP中)
Now I need the new Date(ts)
in an aggregation. (the example is in PHP)
'$ project'=>数组('day'=>'$ new Date(ts)',...
'$project' => array ( 'day' => '$new Date(ts)', ...
'$ group'=>数组("_id" => array('day'=>'$ day)',...),...
'$group' => array ( "_id" => array('day' => '$day)', ...),...
没有得出结果.结果中的天"字段完全丢失.
does not arrive in the results. The field "day" in the result is missing entirely.
如何在聚合中进行转换?
How to get this converted within the aggregation?
推荐答案
为了能够从文档中添加时间戳字段,您可以执行以下操作:
to be able to add a timestamp field from the document, you can do this, given:
{"ts": 1400512120100}
汇总为日期:
db.foo.aggregate({
$project: {
date: { $add: [new Date(0), "$ts"] }
}
})
这篇关于总计$ project中的新Date()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!