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

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

问题描述

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

I am trying to aggregate records in a MongoDB collection 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天全站免登陆