总计 $project 中的新 Date() [英] new Date() in aggregate $project
问题描述
对于我的一个集合,它必须保留 unix 时间戳而不是 isodate,我通常将时间戳转换为 new 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"=>数组('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屋!