Mongo 查询迄今为止的时间戳

问题描述 投票:0回答:1

我正在尝试将 int 时间戳转换为聚合查询中的日期:

[
    {$match: {"source": "tomtom"}},
    {$group: {
        "_id": "$date",
        "count": {
          "$sum": 1
        }
    }},
    {$project: {
        "_id": 0,
        "date":{ $convert: { input: "$_id", to: "date" } },
        "count": 1
    }},
    {$sort: {date:1}}
]

但是,日期是 1970 年 1 月下旬,我怀疑这是因为我的时间戳以秒为单位,而这个函数需要毫秒时间戳。

mongodb mongodb-query aggregation-framework
1个回答
0
投票

解决方案是使用

$multiply
:

{ $multiply : ["$_id", 1000 ]}

完整查询:

[
    {$match: {"source": "tomtom"}},
    {$group: {
        "_id": "$date",
        "count": {
          "$sum": 1
        }
    }},
    {$project: {
        "_id": 0,
        "date":{ $convert: { input: { $multiply : ["$_id", 1000 ]}, to: "date" } },
        "count": 1
    }},
    {$sort: {date:1}}
]
© www.soinside.com 2019 - 2024. All rights reserved.