我应该在MongoDB中将子文档拆分为单独的集合吗?

时间:2019-02-27 10:07:46

标签: mongodb mongoose database-design mongodb-query mongoid

我在MongoDB中设置了以下数据:

{
    "_id" : ObjectId("5c73cc96c2fb92219aeb476a"),
    "menus" : {
        "name" : "Menu",
        "sections" : [ 
            {
                "name" : "Small Plates",
                "items" : [ 
                    {
                        "name" : "Savory Pancakes",
                        "description" : "Choice of vegetarian of seafood options.",
                        "price" : null
                    }, 
                    {
                        "name" : "Kimbop",
                        "description" : "Wrapped in seaweed paper with assorted vegetables. BBQ Beef, Spicy Pork, Chicken, Spicy Fish Cake, Spam (Gluten Free), Spicy Kimchi (Gluten Free), Vegetable (Gluten Free).",
                        "price" : "8.0"
                    }, 
                    {
                        "name" : "Bacon Roll",
                        "description" : "4 pieces. Seasoned rice rolled in bacon strip.",
                        "price" : "8.0"
                    }, 
                    {
                        "name" : "Fried Potstickers",
                        "description" : "8 pieces.",
                        "price" : "8.0"
                    }
                ]
            }, 
            {
                "name" : "Noodles & Soup",
                "items" : [ 
                    {
                        "name" : "Fish Cake Soup (Lunch)",
                        "description" : "Fish cake with radish, glass noodles, and scallions.",
                        "price" : null
                    }, 
                    {
                        "name" : "Beef Short Rib Stew (Lunch)",
                        "description" : "With radish, glass noodles, scallions, and eggs.",
                        "price" : null
                    }, 
                    {
                        "name" : "Korean Gnocchi Soup (Lunch)",
                        "description" : "Korean potato pasta with bean sprouts, squash with eggs, and scallions.",
                        "price" : null
                    }
                ]
            }, 
            {
                "name" : "Rice Dishes",
                "items" : [ 
                    {
                        "name" : "Bibimbop",
                        "description" : "Build your own rice bowl. A bed of rice with spinach, mushroom, radish, julienned carrots, bean sprouts, quinoa and assorted vegetables on top. Topped with your choice of protein and fried egg. Served warm.",
                        "price" : null
                    }, 
                    {
                        "name" : "Kimchi Fried Rice (Lunch)",
                        "description" : "With sunny side up fried egg.",
                        "price" : null
                    }, 
                    {
                        "name" : "Jhap Chae Fried Rice (Lunch)",
                        "description" : "With vegetarian jhap chae and sunny side up fried egg.",
                        "price" : null
                    }
                ]
            }
        ]
    }
}

现在我的问题是: 我应该拆分上述数据集并为菜单,部分和项目创建单独的集合,还是不应该拆分。

我的应用程序中还有另一个功能,用户可以将项目标记为收藏夹,因此在这种情况下,我需要将信息保存在另一个集合中,该集合的列将为“ user_id”和“ item_id”,但是如果我们不拆分数据集,如何获取项目ID作为主键。

0 个答案:

没有答案