在mongodb中表示餐厅菜单的最佳方式是什么?

时间:2014-04-04 17:52:19

标签: node.js mongodb mongoose

尝试为本地应用构建适当的架构,以显示少数餐厅的菜单 所以我在数据库中几乎没有位置,它们都可以有不同的菜单类别和所有不同的项目,你会推荐什么作为在mongodb中表示这个的最佳方式(我使用带有node.js的mongoose)

1 个答案:

答案 0 :(得分:1)

在mongoDB手册上查看Data Model Design

在您的情况下,您可以执行以下操作:

餐厅文件:

{
  _id: <ObjectId1>,
  restaurantname: "123xyz",
  phone: "123456"
  adress: "Adress"
}

类别文件:

{
  _id: <ObjectId2>,
  restaurant_id: <ObjectId1>,
  cat_name: "breakfast"
}

subcat文件:

{
  _id: <ObjectId3>,
  categories_id: <ObjectId2>,
  subcat_name: "Eggs"
}

碟子文件:

{
  _id: <ObjectId4>,
  subcat_id: <ObjectId3>,
  saucer_name: "fried eggs",
  description: "fried eggs description",
  price: "$00.00"
}

在这种情况下检查Data modeling embedding

{
  _id: <ObjectId1>,
  restaurantname: "123xyz",
  phone: "123456"
  adress: "Adress"
  categories:
    {
      _id: <ObjectId2>,
      cat_name: "breakfast",
      subcat:
        {
          _id: <ObjectId3>,
          subcat_name: "Eggs"
          saucer document
            {
              _id: <ObjectId4>,
              saucer_name: "fried eggs",
              description: "fried eggs description",
              price: "$00.00"
            }
        }
    }
}