【问题标题】:How to handle date between India Time on Client side and Server date in US Time via mongo如何通过mongo处理客户端的印度时间和美国时间的服务器日期之间的日期
【发布时间】:2016-12-21 13:47:54
【问题描述】:

我有一个应用程序,其客户位置将在印度。我的应用程序必须根据客户端给出的日期范围聚合数据。因此,如果客户将 2016 年 12 月 14 日至 2016 年 12 月 21 日。它应该在 2016 年 12 月 14 日上午 00:00:00 到 2016 年 12 月 21 日下午 23:59:59 之间搜索。 现在,只要我将日期发送到我的服务器,它就会被转换为
Dec 13 2016 18:30:00 GMT+0000 (UTC)
Dec 21 2016 18:29:59 GMT+0000 (UTC)
现在我将聚合查询编写为

let cursor = Trip.aggregate([{
        $match: {
            startTime: {
                $gte: startDate.toDate(),
                $lte: endDate.toDate()
            },
        }
    },{
        $group: {
            _id: {
                date: {
                    $dayOfMonth: "$startTime"
                },
                month: {
                    $month: "$startTime"
                },
                year: {
                    $year: "$startTime"
                }
            },
            count: {
                $sum: 1
            }
        }
    }]);

这会导致以下输出

[ { _id: { date: 17, month: 12, year: 2016 }, count: 2 },
  { _id: { date: 16, month: 12, year: 2016 }, count: 2 },
  { _id: { date: 13, month: 12, year: 2016 }, count: 2 } ]

旅行发生的实际时间是

"startTime" : ISODate("2016-12-13T20:10:20.381Z")
"startTime" : ISODate("2016-12-13T19:54:56.855Z")

这实际上发生在14-12-2016 01:40:20am14-12-2016 01:24:56am

我希望所有事情都在一个时间范围内,但 MongoDB 不允许在 UTC 以外的任何其他时间范围内存储数据,并且在客户端查询和数据库中管理不同时间变得越来越困难。 我该如何解决呢?

【问题讨论】:

  • 我能想到的一个解决方案是将数据存储为时间戳,以 Epoch 为单位,以毫秒为单位,但这有帮助吗?
  • 我认为你应该在客户端保留时区相关的逻辑。获取客户端所在的时区并将 UTC 日期时间更改回客户端时区。我不确定您在这样做时遇到了什么问题。
  • 问题是mongodb的聚合算法。对于日期为ISODate("2016-12-13T20:10:20.381Z"),它在日期聚合为 2016 年 13 月 12 日。这是正确的。但不是我想要的东西。根据我的说法,您提出的解决方案是将所有数据带到前端并在那里进行所有计算。这将大大增加前端逻辑以及我个人讨厌做的事情。其他方法将放弃 mongodb 聚合框架并在日期上编写我自己的聚合逻辑

标签: mongodb time date-range


【解决方案1】:

您可以通过以下方式进行处理。您可以使用偏移量毫秒保存记录。因此,您的收藏将如下所示。

{
    "_id": ObjectId("585a97dcaceaaa5d2254aeb5"),
    "start_date": ISODate("2016-12-17T00:00:00Z"),
    "offsetmillis": -19080000
} {
    "_id": ObjectId("585a97dcaceaaa5d2254aeb6"),
    "start_date": ISODate("2016-11-17T00:00:00Z"),
    "offsetmillis": -19080000
} {
    "_id": ObjectId("585a97dcaceaaa5d2254aeb7"),
    "start_date": ISODate("2016-11-13T00:00:00Z"),
    "offsetmillis": -19080000
}

您可以在处理时更新聚合查询以包含偏移量毫秒。

aggregate([{
    $match: {
        start_date: {
            $gte: new ISODate("2016-01-01"),
            $lte: new ISODate("2016-12-31")
        },
    }
}, {
    $group: {
        _id: {
            date: {
                $dayOfMonth: {
                    $add: ["$start_date", "$offsetmillis"]
                }
            },
            month: {
                $month: {
                    $add: ["$start_date", "$offsetmillis"]
                }
            },
            year: {
                $year: {
                    $add: ["$start_date", "$offsetmillis"]
                }
            }
        },
        count: {
            $sum: 1
        }
    }
}]);

示例响应

{ "_id" : { "date" : 12, "month" : 11, "year" : 2016 }, "count" : 1 }
{ "_id" : { "date" : 16, "month" : 11, "year" : 2016 }, "count" : 1 }
{ "_id" : { "date" : 16, "month" : 12, "year" : 2016 }, "count" : 1 }

您可以对其进行更多优化,但我认为这会给您一个想法。

【讨论】:

  • 像魅力一样工作。谢谢朋友
猜你喜欢
  • 2013-04-19
  • 2013-10-30
  • 1970-01-01
  • 2020-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
相关资源
最近更新 更多