将日期从毫秒转换为ISODate对象 [英] Convert date from milliseconds to ISODate object

查看:268
本文介绍了将日期从毫秒转换为ISODate对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按小时聚合MongoDB colloection中的记录,并且需要将存储为timestamp(毫秒)的日期转换为ISODate,以便我可以使用聚合框架的内置日期运算符($ hour,$ month等)

I am trying to aggregate records in a MongoDB colloection by hour and need to convert date stored as timestamp (milliseconds) to ISODate so that I can use aggregate framework's built-in date operators ($hour, $month, etc.)

记录存储为

{ 
"data" : { "UserId" : "abc", "ProjId" : "xyz"}, 
"time" : NumberLong("1395140780706"),
"_id" : ObjectId("532828ac338ed9c33aa8eca7") 
} 

我正在尝试使用以下类型的聚合查询:

I am trying to use an aggregate query of following type:

db.events.aggregate(
    { 
       $match : { 
         "time" : { $gte : 1395186209804, $lte : 1395192902825 } 
       } 
    }, 
    { 
       $project : {
         _id : "$_id", 
         dt : {$concat : (Date("$time")).toString()} // need to project as ISODate
       } 
    },
    // process records further in $project or $group clause
)

产生表格的结果:

{
    "result" : [
        { 
            "_id" : ObjectId("5328da21fd207d9c3567d3ec"), 
            "dt" : "Fri Mar 21 2014 17:35:46 GMT-0400 (EDT)" 
        }, 
        { 
            "_id" : ObjectId("5328da21fd207d9c3567d3ed"), 
            "dt" : "Fri Mar 21 2014 17:35:46 GMT-0400 (EDT)" 
        }, 
            ... 
} 

我想提取小时,日期,月份和年份从日期开始,但由于时间作为字符串向前推进,我无法使用聚合框架的内置日期运算符($ hour等)。

I want to extract hour, day, month, and year from the date but since time is projected forward as string I am unable to use aggregate framework's built-in date operators ($hour, etc.).

如何将时间从毫秒转换为ISO日期,以便执行以下操作:

How can I convert time from milliseconds to ISO date to do sometime like the following:

db.events.aggregate(
    {
        $match : { 
            "time" : { $gte : 1395186209804, $lte : 1395192902825 } 
        }
    },
    {
        $project : {
            _id : "$_id",
            dt : <ISO date from "$time">
        }
    },
    { 
        $project : {
            _id : "$_id",
            date : { 
                hour : {$hour : "$dt"} 
            }
        }
    }
)


推荐答案

我认为没有办法做到这一点。因为聚合框架是用本机代码编写的。没有使用V8引擎。因此,JavaScript的所有内容都无法在框架内运行(这也是聚合框架运行速度更快的原因)。

Map / Reduce是一种解决这个问题的方法,但聚合框架肯定会有更好的性能。

I assume there's no way to do it. Because aggregation framework is written in native code. not making use of the V8 engine. Thus everything of JavaScript is not gonna work within the framework (And that's also why aggregation framework runs much faster).
Map/Reduce is a way to work this out, but aggregation framework definitely got much better performance.

关于Map / Reduce性能,请阅读这个主题

About Map/Reduce performance, read this thread.

另一种解决方法是从聚合框架中获取原始结果,把它放到一个JSON数组中。然后通过运行JavaScript进行转换。类似于:

Another way to work it out would be get a "raw" result from aggregation framework, put it into an JSON array. Then do the conversion by running JavaScript. Sort of like:

var results = db.events.aggregate(...);
reasult.forEach(function(data) {
    data.date = new Date(data.dateInMillionSeconds);
    // date is now stored in the "date" property
}

这篇关于将日期从毫秒转换为ISODate对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆