MongoDB汇总数据的正确架构 [英] MongoDB Correct Schema for aggregated data

查看:99
本文介绍了MongoDB汇总数据的正确架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的馆藏,其中包含大量统计数据,因为我想生成报告,因此我每天运行一次cron,它将来自主要馆藏的数据汇总到较小的数据中.问题是:存储所有汇总数据的正确方法是什么?

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. the thing is: what is the correct way to store all the aggregated data?

方法1:

{
    'Y2015': {
        'M04': {
            'D18': 100,
            'D19': 200
        }
    },
    'order_id': 'VjprK',
    'user_id': '777'
}

{
    'Y2015': {
        'M04': {
            'D18': 100,
            'D19': 20
        }
    },
    'order_id': 'LaOPX',
    'user_id': '777'
}

{
    'Y2015': {
        'M04': {
            'D18': 100,
            'D19': 50
        }
    },
    'order_id': 'cYwxf',
    'user_id': '777'
}

方法2:

{
    'order_id': 'VjprK',
    'user_id': '777',
    data {
        'MongoDate(2015-04-18)' : 100,
        'MongoDate(2015-04-19)' : 200,
        'MongoDate(2015-04-20)' : 300,
        'MongoDate(2015-04-21)' : 400,
    }
}

将来,我想按日期范围进行查询,因此感觉就像方法2.

In the future, I want to query by date ranges, so it feels like method 2.

有什么建议吗?

推荐答案

建议进一步重组方法2中的架构以遵循以下架构:

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
        }
    ]
}

,然后可以在给定的日期范围内进行汇总,例如从2015-04-182015-04-19.考虑以下管道:

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"
            }
        }
    }    
])

示例输出

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

这篇关于MongoDB汇总数据的正确架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆