我如何设计架构由数组组成

时间:2019-11-09 15:08:28

标签: mongodb

让我们说,我有一个样本数组将提供给我的锦标赛括号,以便它生成锦标赛bracet,其中样本数组由多轮数组组成,其中内部具有轮名和特定轮次的匹配。我如何设计架构,以便可以像这样传递示例数组?

(样本数组)

//For 16 players
    var matchInfo = {
        "rounds" : [
            { "name": "Round1",
                "matches" : [
                    { "id" : 1, "p1" : "Team A", "p2" : "Team I" },
                    { "id" : 2, "p1" : "Team B", "p2" : "Team J" },
                    { "id" : 3, "p1" : "Team C", "p2" : "Team K" },
                    { "id" : 4, "p1" : "Team D", "p2" : "Team L" },
                    { "id" : 5, "p1" : "Team E", "p2" : "Team M" },
                    { "id" : 6, "p1" : "Team F", "p2" : "Team N" },
                    { "id" : 7, "p1" : "Team G", "p2" : "Team O" },
                    { "id" : 8, "p1" : "Team H", "p2" : "Team P" }
                ]
            },
            { "name": "Round2",
                "matches" : [
                    { "id" : 9, "p1" : null, "p2" : null },
                    { "id" : 10, "p1" : null, "p2" : null },
                    { "id" : 11, "p1" : null, "p2" : null },
                    { "id" : 12, "p1" : null, "p2" : null }
                ]
            },
            { "name": "Round3",
                "matches" : [
                    { "id" : 13, "p1" : null, "p2" : null },
                    { "id" : 14, "p1" : null, "p2" : null },
                ]
            },
            { "name": "Round4",
                "matches" : [
                    { "id" : 15, "p1" : null, "p2" : null },
                ]
            } 
        ]
    };

//For 8 players
    var matchInfo = {
        "rounds" : [
            { "name": "Round1",
                "matches" : [
                    { "id" : 1, "p1" : "Team A", "p2" : "Team E" },
                    { "id" : 2, "p1" : "Team B", "p2" : "Team F" },
                    { "id" : 3, "p1" : "Team C", "p2" : "Team G" },
                    { "id" : 4, "p1" : "Team D", "p2" : "Team H" },
                ]
            },
            { "name": "Round2",
                "matches" : [
                    { "id" : 9, "p1" : null, "p2" : null },
                    { "id" : 10, "p1" : null, "p2" : null },
                ]
            },
            { "name": "Round3",
                "matches" : [
                    { "id" : 13, "p1" : null, "p2" : null },
                ]
            },
        ]
    };

//For 4 players
    var matchInfo = {
        "rounds" : [
            { "name": "Round1",
                "matches" : [
                    { "id" : 1, "p1" : "Team A", "p2" : "Team C" },
                    { "id" : 2, "p1" : "Team B", "p2" : "Team D" },
                ]
            },
            { "name": "Round2",
                "matches" : [
                    { "id" : 9, "p1" : null, "p2" : null },
                ]
            },
        ]
    };

更新

let mongoose = require('mongoose');

// Bracket Schema
let BracketSchema = mongoose.Schema({
    tournamentId:{
        type: String
    },
    match_info:{
        rounds: 
        [{
            name: { 
                type: String 
            },
            matches: [{
                id: { 
                    type: Number 
                },
                p1: { 
                    type: String 
                },
                p2: { 
                    type: String 
                }
            }]
        }]
    },
    created_at: {
        type: Date, 
        default: Date.now
    }
});

let Bracket = module.exports = mongoose.model('Bracket', BracketSchema);

1 个答案:

答案 0 :(得分:0)

使用mongoose定义架构

https://mongoosejs.com/docs/schematypes.html

// option1
const schema = new Schema({
  rounds:   Schema.Types.Mixed
});

// option2
const schema = new Schema({
  rounds:  [{
    name: { type: String, required: true },
    matches: []
  }]
});

// option3
const schema = new Schema({
  rounds:  [{
    name: { type: String, required: true },
    matches: [{
        id: { type: Number, required: true },
        p1: { type: String },
        p2: { type: String },
    }]
  }]
});