我正在构建酒店预订应用程序。制作一个非常简单的应用程序所需的信息是:一个旅馆,属于每个旅馆的房间以及属于每个房间的预订。
以嵌套为例,它看起来(忽略一些_id不是唯一的,这只是一个快速演示):
{"_id":"5c8639a856be210634dfcf16",
"hotelName":"Hotel A",
"hotelDescription":"The best hotel ever",
"rooms":{
{"_id":"1235e56be210634dfcf13",
"roomNumber":"1",
"bookable: True",
"reservations":{
{"_id":"5c86365e56be21a634dfasdf",
"from":"2019-01-01",
"to": "2019-01-06"},
{"_id":"5c86365e56be21a634dfasdf",
"from":"2019-01-06",
"to": "2019-01-10"},
{"_id":"123455e56be21a634dfasdf",
"from":"2019-01-15",
"to": "2019-01-20"},
}
},
{"_id":"1235e56be210634dfcf13",
"roomNumber":"2",
"bookable: True",
"reservations":{
{"_id":"5c86365e56be21a634dfasdf",
"from":"2019-01-01",
"to": "2019-01-06"},
{"_id":"5c86365e56be21a634dfasdf",
"from":"2019-01-06",
"to": "2019-01-10"},
{"_id":"123455e56be21a634dfasdf",
"from":"2019-01-15",
"to": "2019-01-20"},
}
}
}
如您所见,房间被嵌套在酒店中,预订被嵌套在房间中。这将永远不会更深。一个普通的酒店可以有400间客房,平均每年50个预订。
但是。我想知道我应该对此进行非规范化吗?
那么简单: (所有_id由Mongodb自动生成)
Hotels
Hotel _id:1,
Hotel _id:2,
Hotel _id:3,
Rooms
Hotel _id:1
Room _id:1,
Room _id:2,
Room _id:3,
Hotel _id:2
Room _id:4,
Room _id:5,
Room _id:6,
Reservations:
Room _id:1
Reservation _id:1,
Reservation _id:2,
Reservation _id:3,
Room _id:2
Reservation _id:4,
Reservation _id:5,
Reservation _id:6
答案 0 :(得分:0)
以下架构可以帮助您实现要求,
/* 1 */
{
"_id" : 1.0,
"hotelname" : "hotel1",
"rooms" : [
{
"roomno" : "room11",
"reservations" : [
{
"reservationid" : "reservation111",
"startDate" : "2019-03-18",
"endDate" : "2019-03-20"
},
{
"reservationid" : "reservation112",
"startDate" : "2019-03-21",
"endDate" : "2019-03-25"
}
]
},
{
"roomno" : "room12",
"reservations" : [
{
"reservationid" : "reservation121",
"startDate" : "2019-03-15",
"endDate" : "2019-03-18"
},
{
"reservationid" : "reservation122",
"startDate" : "2019-03-19",
"endDate" : "2019-03-21"
}
]
}
]
}
/* 2 */
{
"_id" : 2.0,
"hotelname" : "hotel2",
"rooms" : [
{
"roomno" : "room21",
"reservations" : [
{
"reservationid" : "reservation211",
"startDate" : "2019-03-18",
"endDate" : "2019-03-20"
},
{
"reservationid" : "reservation212",
"startDate" : "2019-03-21",
"endDate" : "2019-03-25"
}
]
},
{
"roomno" : "room22",
"reservations" : [
{
"reservationid" : "reservation221",
"startDate" : "2019-03-15",
"endDate" : "2019-03-18"
},
{
"reservationid" : "reservation222",
"startDate" : "2019-03-19",
"endDate" : "2019-03-21"
}
]
}
]
}