V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
lvming6816077
V2EX  ›  Node.js

mongoose 按日期分组查询问题

  •  
  •   lvming6816077 · Apr 20, 2022 · 6106 views
    This topic created in 1479 days ago, the information mentioned may be changed or developed.

    表的结构:

    {
            "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 replies    2022-04-21 08:42:37 +08:00
    catinsides
        1
    catinsides  
       Apr 20, 2022
    我的理解是需要先在程序里初始化一些没有数据的日期范围吧?不然数据库也无法知道“没有数据的日期”是什么。
    chenzhe
        2
    chenzhe  
    PRO
       Apr 20, 2022
    标记一下,也遇到了这个问题。
    tanrunhao
        3
    tanrunhao  
       Apr 20, 2022 via Android
    这个不应该在 mongoose 上做 应该在 js 代码自己补一下
    itechify
        4
    itechify  
    PRO
       Apr 20, 2022 via Android
    很像 mysql 的分组统计问题,mysql 是先搞出日期表作为主表,join 分组后的表,如果 null 设置 0

    觉得这种场景在后端程序来的更快捷,后端一次循环,免去一大段 sql
    lvming6816077
        5
    lvming6816077  
    OP
       Apr 21, 2022
    @oneisall8955 感谢回答
    lvming6816077
        6
    lvming6816077  
    OP
       Apr 21, 2022
    @tanrunhao 那这样在 service 层处理一下
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3881 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 00:47 · PVG 08:47 · LAX 17:47 · JFK 20:47
    ♥ Do have faith in what you're doing.