我正在制作NBA API。目前,我已经为团队,球员,赛季和比赛设置了模型(我遇到麻烦的模型)。
since I’m used to relational databases/normalized data- I’m using foreign keys/references and then using mongooses populate fill out the data fully on user request- this works but is definitely not efficient- however populate does allow me to pick and choose certain attributes of each model to display which is very useful- as well as giving user access to the actual document’s ID should they want more information on a particular player/team/etc
games look something like:
{
home_team: (reference to team model- populated w/ JUST name/conference),
away_team: (reference to team model- populated w/ JUST name/conference),
season: (ref to season model populated w/ JUST year)
boxscore: {
homeBox:[
(bunch of random stats),
player: reference to player model(populated w/ JUST name)
]
awayBox:[
(bunch of random stats),
player: reference to player model(populated w/ JUST name)
]
}
}
so when a user wants to fetch a specific game:
the game needs to be found
the hometeam needs to be found and its data is used to populate
the awayteam needs to be found and its data is used to populate
the season needs to be found and its data is used to populate
EVERY single player within the boxscore needs to be found and their data used to populate (~60 times for each game)
eventually there will be 20k+ games in the database
how expensive are these queries to make? As opposed to using foreign keys and populating everything am I better off duplicating the data in some spots (ie a game could have an attribute for the year it was played-- and never have to reference the season model) -- if I were to take this approach- what is the conventional way to do so? Just forego any connection between the models? OR maybe have some sort of embedded document that contains fields just like the model its “representing?” Just looking to get some guidance on how to approach this before I start playing with huge numbers of documents in the database.
谢谢
游戏示例查询
games = await Game.find({}).populate('home_team', 'teamCode city fullName conference division').populate('away_team', 'teamCode city fullName conference division').populate({
path: 'season',
match,
select: 'year description'
}