首页 > 解决方案 > MongoDB 效率最佳模式设计

问题描述

所以我对 mongoDB 还很陌生,但使用过 sql 相当多。我希望这是一个相当简单的问题,所以我会陷入困境。

在以下两种模式设计中,哪一种是最有效/最容易查询的?我将做相当简单的查询(我希望如此)。“销售”对象将比我在下面概述的要复杂得多。数据库中将有大约 30 家商店,预计在几年内会增长到 60 家。

    {
        name: "shop1",
        sales:
            [
                {
                    weekEnding: "5/1/2020",
                    sales: 156.50,
                    customers: 5
                },
                {
                    weekEnding: "12/1/2020",
                    sales: 256.50,
                    customers: 12
                },
            ]
    },
    {
        name: "shop2",
        sales:
            [
                {
                    weekEnding: "5/1/2020",
                    sales: 456.50,
                    customers: 9
                },
                {
                    weekEnding: "12/1/2020",
                    sales: 446.50,
                    customers: 19
                },
            ]
    },

    {
        name: "shop1",
        weekEnding: "5/1/2020",
        sales: 156.50,
        customers: 5
    },
    {

        name: "shop1",
        weekEnding: "12/1/2020",
        sales: 256.50,
        customers: 12
    },

    {
        name: "shop2",
        weekEnding: "5/1/2020",
        sales: 456.50,
        customers: 9
    },
    {
        name: "shop2",
        weekEnding: "12/1/2020",
        sales: 446.50,
        customers: 19
    },

标签: javascriptmongodb

解决方案


我不是 MongoDB 专家,但我认为使用选项 2 会更好。使用选项 1,您的数据库中只有大约 30 个文档,并且所有文档都可能变得非常大,我认为 MongoDB 将不得不在磁盘上重新排列它们当它们改变大小时,因为对象应该保存在连贯的内存空间中。

而且我认为选项 2 没有任何问题。它可以很好地按商店索引销售额,并且不会导致任何性能问题。


推荐阅读