V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lvming6816077
V2EX  ›  Node.js

mongoose 按日期分组查询问题

  •  
  •   lvming6816077 · 2022-04-20 15:37:25 +08:00 · 5448 次点击
    这是一个创建于 730 天前的主题,其中的信息可能已经有所发展或是发生改变。

    表的结构:

    {
            "point": "625fa3ccd3ebc0de0f1837d9",
            "_id": "625fb7ad9c9d58cdc664fde5",
            "create": "2022-04-20T07:35:09.203Z",
            "update": "2022-04-20T07:35:09.203Z",
            "__v": 0
        }
    

    现在需要按照时间,每分钟统计次数,得到如下 aggregate:

    this.reportModel.aggregate([
                    {$match: {point:data[0]._id,}},
                    {$project: {
                        date1Str: {$dateToString: {format: "%Y-%m-%d %H:%M", date:"$create",timezone:"+08:00"}},
                     }},
                    {$group: {_id: "$date1Str", total: {$sum: 1}}},
                    {$sort:{_id:1}}
                ]).exec()
    

    查出来的数据,只统计了有数据的日期

    [
            {
                "_id": "2022-04-20 14:10",
                "total": 8
            },
            {
                "_id": "2022-04-20 14:11",
                "total": 1
            },
            {
                "_id": "2022-04-20 14:19",
                "total": 1
            },
            {
                "_id": "2022-04-20 14:20",
                "total": 6
            },
            {
                "_id": "2022-04-20 14:25",
                "total": 8
            },
            {
                "_id": "2022-04-20 15:16",
                "total": 1
            },
            {
                "_id": "2022-04-20 15:35",
                "total": 1
            }
        ]
    

    如何实现没有数据的日期自动补 0 ,例如:

    [
            {
                "_id": "2022-04-20 14:10",
                "total": 8
            },
            {
                "_id": "2022-04-20 14:11",
                "total": 0
            },
            {
                "_id": "2022-04-20 14:12",
                "total": 0
            },
            {
                "_id": "2022-04-20 14:11",
                "total": 1
            },
            ...
    
        ]
    
    6 条回复    2022-04-21 08:42:37 +08:00
    catinsides
        1
    catinsides  
       2022-04-20 16:11:52 +08:00
    我的理解是需要先在程序里初始化一些没有数据的日期范围吧?不然数据库也无法知道“没有数据的日期”是什么。
    chenzhe
        2
    chenzhe  
       2022-04-20 16:59:30 +08:00
    标记一下,也遇到了这个问题。
    tanrunhao
        3
    tanrunhao  
       2022-04-20 19:22:58 +08:00 via Android
    这个不应该在 mongoose 上做 应该在 js 代码自己补一下
    oneisall8955
        4
    oneisall8955  
       2022-04-20 19:47:35 +08:00 via Android
    很像 mysql 的分组统计问题,mysql 是先搞出日期表作为主表,join 分组后的表,如果 null 设置 0

    觉得这种场景在后端程序来的更快捷,后端一次循环,免去一大段 sql
    lvming6816077
        5
    lvming6816077  
    OP
       2022-04-21 08:42:14 +08:00
    @oneisall8955 感谢回答
    lvming6816077
        6
    lvming6816077  
    OP
       2022-04-21 08:42:37 +08:00
    @tanrunhao 那这样在 service 层处理一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1137 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:21 · PVG 02:21 · LAX 11:21 · JFK 14:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.