mongodbdata-modelingtournament

Modelling tournament brackets in MongoDB


I've been experimenting with MongoDB in order to move some parts of an app to it. I'm thinking a document-based db like mongodb would be a nice fit for tournament brackets but I'm having sort of a hard time coming up with a suitable model. (still trying to break free from RDBMS dogma)

Anyone have any ideas for a good way to model Single AND Double-elimination tournament brackets?


Solution

  • Both tournament variation basically come down to each match either resulting in one of these options :

    So, if you model it so that you have a collection of matches with a schema like :

    {
    _id :.., <- match id
    players:[playerId1, playerId2],
    resultForWinner: <either "WINS_TOURNAMENT" or match id of next match>
    resultForLoser: <either "EXIT_TOURNAMENT" or match id of loser bracket match
    }
    

    You can compose both types of tournament brackets with this schema and reuse your tournament logic without making a distinction between the two other than bracket setup.