简体   繁体   English

您如何从Mongo日期字段获取DayHours?

[英]How do you get DayHours from Mongo date field?

I am trying to group by DayHours in a mongo aggregate function to get the past 24 hours of data. 我正在尝试在Mongo汇总函数中将DayHours分组,以获取过去24小时的数据。 For example: if the time of an event was 6:00 Friday the "DayHour" would be 6-5. 例如:如果活动时间是星期五6:00,则“ DayHour”将是6-5。 I'm easily able to group by hour with the following query: 我可以通过以下查询按小时分组:

db.api_log.aggregate([
    { '$group': { 
        '_id': { 
            '$hour': '$time'
        }, 
        'count': { 
          '$sum':1 
        } 
      } 
    },
    { '$sort' : { '_id': -1 } }
  ])

I feel like there is a better way to do this. 我觉得有更好的方法可以做到这一点。 I've tried concatenation in the $project statement, however you can only concatenate strings in mongo(apparently). 我已经尝试在$ project语句中进行串联,但是您只能在mongo(显然)中串联字符串。 I effectively just need to end up grouping by day and hour, however it gets done. 实际上,我只需要按日和小时分组即可,但是已经完成了。 Thank You. 谢谢。

I assume that time field contains ISODate . 我认为time字段包含ISODate If you want only last 24 hours you can use this: 如果您只希望持续24小时,则可以使用以下方法:

var yesterday = new Date((new Date).setDate(new Date().getDate() - 1));

db.api_log.aggregate(
    {$match: {time: {$gt: yesterday}}},
    {$group: {
        _id: {
            hour: {$hour: "$time"},
            day: {$dayOfMonth: "$time"},
        },
        count: {$sum: 1}
    }}
)          

If you want general grouping by day-hour you can use this: 如果要按日进行常规分组,可以使用以下方法:

db.api_log.aggregate(
    {$group: {
        _id: {
            hour: {$hour: "$time"},
            day: {$dayOfMonth: "$time"},
            month: {$month: "$time"},
            year: {$year: "$time"}
        },
        count: {$sum: 1}
    }}
)

Also this is not an answer per se (I do not have mongodb now to come up with the answer), but I think that you can not do this just with aggregation framework (I might be wrong, so I will explain myself). 同样,这本身并不是一个答案(我现在没有mongodb来提供答案),但是我认为您不能仅使用聚合框架来做到这一点(我可能是错的,所以我会自己解释一下)。

You can obtain date and time information from mongoId using .getTimestamp method. 您可以使用.getTimestamp方法从mongoId获取日期和时间信息。 The problem that you can not output this information in mongo query (something like db.find({},{_id.getTimestamp}) does not work). 无法在mongo查询中输出此信息的问题(类似于db.find({},{_id.getTimestamp})不起作用。 You also can not search by this field (except of using $where clause). 您也不能通过此字段进行搜索(使用$ where子句除外)。

So if it is possible to achieve, it can be done only using mapreduce, where in reduce function you group based on the output of getTimestamp . 因此,如果有可能实现,则只能使用mapreduce完成,在reduce函数中,您可以根据getTimestamp的输出对它们进行分组。

If this is the query you are going to do quite often I would recommend actually adding date field to your document, because using this field you will be able properly aggregate your data and also you can use indeces not to scan all your collection (like you are doing with $sort -1, but to $match only the part which is bigger then current date - 24 hours ). 如果这是您经常要执行的查询,我建议您实际在文档中添加date字段,因为使用此字段,您将能够正确地汇总数据,并且还可以使用indees不扫描所有集合(例如您与$ sort -1一起使用,但要$match$match大于current date的部分24 hours )。

I hope this can help even without a code. 我希望即使没有代码也能有所帮助。 If no one will be able to answer this, I will try to play with it tomorrow. 如果没有人能够回答这个问题,我明天将尝试使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM