【发布时间】:2018-09-05 00:48:17
【问题描述】:
我正在使用 Mongoose 来设计模式。我有两个名为 Restaurants 和 Products 的集合。我一直在 Restaurant Collection 中存储所有餐厅的详细信息。在 Products 集合中,我有一个名为 restaurantCode 的字段,它在 Restaurant 集合中显示为 _id 并映射了两者收藏成功。现在在前端使用 Angular 5,我已经将控制权交给了餐厅老板,让他们按照他们的意愿对产品进行分类。为了执行此操作,我在 Products 集合中创建了一个名为 sortIndex 的新字段,并分配了从 0 到最多每家餐厅可用产品数量的值。在下面发布我的虚拟数据
餐厅详情
1)
_id : "ABC",
"Name" : "Eat Well Restaurant",
2)
_id : "DEF",
"Name" : "Eat ALL Restaurant",
产品详情
1)
"name": "Pizza",
"Price": 20,
"restaurantCode" : "ABC",
"sortIndex": 0
2)
"name": "Juice",
"Price": 30,
"restaurantCode" : "ABC",
"sortIndex": 1
3)
"name": "Fruit",
"Price": 20,
"restaurantCode" : "ABC",
"sortIndex": 2
4)
"name": "Chicken Rice",
"Price": 20,
"restaurantCode" : "DEF",
"sortIndex": 0
5)
"name": "Chicken Fry",
"Price": 20,
"restaurantCode" : "DEF",
"sortIndex": 1
6)
"name": "Chicken Briyani",
"Price": 20,
"restaurantCode" : "DEF",
"sortIndex": 2
基于 sortIndex 值显示产品。现在,每当用户在前端(Angular)上执行排序操作时,我都会重新分配 sortIndex 值。问题是每当餐厅所有者删除产品时,我必须为与特定餐厅关联的所有产品重新分配 sortIndex 值。我觉得这是一个昂贵的操作。这是我到目前为止一直在使用的方法,并且效果很好。我想知道有没有更好的方法来解决这个问题,我相信会有一个。我的堆栈是 NodeJS、Mongoose、MongoDB、Angular(Front-End) 留下你的想法/想法。谢谢
【问题讨论】:
标签: node.js angular mongodb mongoose