您可以更好地利用MongoDB中允許您使用的面向文檔的體系結構,而不是試圖在MongoDB中強制實施關係模型(面向列,在單值字段中進行翻譯,這可以從您的描述中理解)在你的架構對象:
{
_id: ObjectId("5805d576adf2ac885283779a"),
// Name of the room
room_name: 'Hold Them Up',
// When the room was created
created_at: ISODate("2016-10-18T08:04:17.611Z"),
// The uptime of the room in seconds or any other unit
uptime: 2000,
// You could organize this as an object that contains values for
// each of the turning phases of the game to allow for better analytics
current_table_cards: {
flop: ["A", "B", "C"],
turn: "D",
river: "E"
},
// You could also keep track of previous table cards
table_cards_history: [
{
flop: ["A", "B", "C"],
turn: "D",
river: "E"
},
{
flop: ["E", "D", "C"],
turn: "B",
river: "A"
},
...
],
// You could use an array to store the players that are currently playing
// and save a history of their previous 5 hands for example
//
// To track the order of the players, you can either manipulate this array
// and consider the indices the order of the players, or you could define
// an order property on the objects inside this array
players: [
{
user_id: ObjectId("5805d576adf2ac8852837791"),
cash_amount: 3201,
position: 2,
win: 1,
loss: 0,
current_hand: ["E", "F"],
hands_history: [
["A", "B"],
["A", "A"],
...
]
},
{
user_id: ObjectId("5805d576adf2ac8852837792"),
cash_amount: 4288,
position: 1,
win: 2,
loss: 1,
current_hand: ["C", "D"],
hands_history: [
["A", "E"],
["B", "B"],
...
]
},
{
user_id: ObjectId("5805d576adf2ac8852837793"),
cash_amount: 2531,
position: 3,
win: 0,
loss: 2,
current_hand: ["A", "B"],
hands_history: [
["D", "D"],
["C", "C"],
...
]
},
...
],
// Blind information
small_blind: ObjectId("5805d576adf2ac8852837792"),
big_blind: ObjectId("5805d576adf2ac8852837791"),
blind_amount: 100
}
這僅僅是爲您的應用程序最終可能有很多更多的領域,這取決於您要跟蹤每個房間和玩家信息的初始模型。
例如,您可能想要跟蹤玩家的平均下注量,或者爲盲注更改時設置計時器。然而,這超出了這個問題的範圍,是一個不同的討論。
此外,由於Sergio Tulentsev在他的評論中提到,根據您需要提取的信息優化您的數據模型非常重要。因此,根據應用程序需求,您必須在數據模型和查詢訪問模式之間找到平衡,以優化性能。
「我該如何做得更好」 - 你認爲這個不好嗎? –
是的,我覺得IT可以做得更好@SergioTulentsev – maria
更好的是什麼?沒有抽象的「更好」 –