MongoDB Correct Schema for aggregated data

后端 未结 4 1233
旧时难觅i
旧时难觅i 2020-12-22 05:31

I have a big collection that holds lots of stats, since I want to generate reports, I am running a daily cron which aggregates data from the main collection to a smaller one

4条回答
  •  無奈伤痛
    2020-12-22 05:48

    Would recommend further restructuring the schema in Method 2 to follow this schema:

    /* 0 */
    {
        "_id" : ObjectId("5577fd322ab13c8cacdd0e70"),
        "order_id" : "VjprK",
        "user_id" : "777",
        "data" : [ 
            {
                "order_date" : ISODate("2015-04-18T08:57:42.514Z"),
                "amount" : 100
            }, 
            {
                "order_date" : ISODate("2015-04-19T08:57:42.514Z"),
                "amount" : 200
            }, 
            {
                "order_date" : ISODate("2015-04-20T08:57:42.514Z"),
                "amount" : 300
            }, 
            {
                "order_date" : ISODate("2015-04-21T08:57:42.514Z"),
                "amount" : 400
            }
        ]
    }
    

    which you can then aggregate with a given date range, say from 2015-04-18 to 2015-04-19. Consider the following pipeline:

    var start = new Date(2015, 3, 18),
        end = new Date(2015, 3, 20);
    
    db.orders.aggregate([
        {
            "$match": {
                "user_id": "777",
                "data.order_date": {
                    "$gte": start,
                    "$lt": end
                }
            }
        },
        {
            "$unwind": "$data"
        },
        {
            "$match": {
                "data.order_date": {
                    "$gte": start,
                    "$lt": end
                }
            }
        },
        {
            "$group": {
                "_id": "$user_id",
                "total": {
                    "$sum": "$data.amount"
                }
            }
        }    
    ])
    

    Sample Output

    /* 0 */
    {
        "result" : [ 
            {
                "_id" : "777",
                "total" : 300
            }
        ],
        "ok" : 1
    }
    

提交回复
热议问题