Search code examples
mongodbmongodb-.net-drivermongodb-query

MongoDB database relationships


I have just started to build a database in MongoDB. I have 2 tables, Teams and Fixtures, and each Fixture document should have 2 links to the Fixtures table, a HomeTeamId and an AwayTeamId. Is this the correct way to do this?

Teams :

{
    "_id": {
        "$oid": "542978c4e4b0e67da1edc7f3"
    },
    "TeamName": "Arsenal",
    "BadgeSmall": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Small/arsenal.png",
    "BadgeLarge": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Large/arsenal.png",
    "TeamImage": "http://footballcomps.azurewebsites.net/Content/Images/Teams/arsenal.png",
    "Formation": "http://footballcomps.azurewebsites.net/Content/Images/Formations/arsenal.png"
}

{
    "_id": {
        "$oid": "542979c3e4b0e67da1edc807"
    },
    "TeamName": "Aston Villa",
    "BadgeSmall": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Small/astonvilla.png",
    "BadgeLarge": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Large/astonvilla.png",
    "TeamImage": "http://footballcomps.azurewebsites.net/Content/Images/Teams/astonvilla.png",
    "Formation": "http://footballcomps.azurewebsites.net/Content/Images/Formations/astonvilla.png"
}

{
    "_id": {
        "$oid": "542c45eee4b0413a3dd4d43e"
    },
    "TeamName": "Crystal Palace",
    "BadgeSmall": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Small/crystalpalace.png",
    "BadgeLarge": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Large/crystalpalace.png",
    "TeamImage": "http://footballcomps.azurewebsites.net/Content/Images/Teams/crystalpalace.png",
    "Formation": "http://footballcomps.azurewebsites.net/Content/Images/Formations/crystalpalace.png"
}

Fixtures :

Home Team : Aston Villa, Away Team : Arsenal :

{
    "_id": {
        "$oid": "542f0dfbe4b0413a3dd4ead4"
    },
    "Date": "2014-09-20",
    "HomeTeamId": "542979c3e4b0e67da1edc807",
    "AwayTeamId": "542978c4e4b0e67da1edc7f3",
    "HomeTeamScore": 0,
    "AwayTeamScore": 3,
    "HomeTeamScorers": "",
    "AwayTeamScorers": "Ozil 33,Welbeck 34,Cissokho 35 (OG)"
}

Home Team : Arsenal, Away Team : Crystal Palace :

{
    "_id": {
        "$oid": "542f0f54e4b0413a3dd4ead8"
    },
    "Date": "2014-08-16",
    "HomeTeamId": "542978c4e4b0e67da1edc7f3",
    "AwayTeamId": "542c45eee4b0413a3dd4d43e",
    "HomeTeamScore": 2,
    "AwayTeamScore": 1,
    "HomeTeamScorers": "Koscielny 45,Ramsey 90",
    "AwayTeamScorers": "Hangeland 35"
}

Solution

  • I personally would suggest embedding the team objects within the game The team and their icons are unlikely to change within a season. Makes 1 call instead of 3.

    {
        "_id": {
            "$oid": "542f0dfbe4b0413a3dd4ead4"
        },
        "Date": "2014-09-20",
        "HomeTeamId": "542979c3e4b0e67da1edc807",
        "AwayTeamId": "542978c4e4b0e67da1edc7f3",
        "HomeTeamScore": 0,
        "AwayTeamScore": 3,
        "HomeTeamScorers": "",
        "AwayTeamScorers": "Ozil 33,Welbeck 34,Cissokho 35 (OG)",
        "HomeTeam" : {
            "_id": {
                "$oid": "542979c3e4b0e67da1edc807"
            },
            "TeamName": "Aston Villa",
            "BadgeSmall": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Small/astonvilla.png",
            "BadgeLarge": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Large/astonvilla.png",
            "TeamImage": "http://footballcomps.azurewebsites.net/Content/Images/Teams/astonvilla.png",
            "Formation": "http://footballcomps.azurewebsites.net/Content/Images/Formations/astonvilla.png"
        },
        "AwayTeam" : {
                "_id": {
                    "$oid": "542978c4e4b0e67da1edc7f3"
                },
                "TeamName": "Arsenal",
                "BadgeSmall": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Small/arsenal.png",
                "BadgeLarge": "http://footballcomps.azurewebsites.net/Content/Images/Badges/Large/arsenal.png",
                "TeamImage": "http://footballcomps.azurewebsites.net/Content/Images/Teams/arsenal.png",
                "Formation": "http://footballcomps.azurewebsites.net/Content/Images/Formations/arsenal.png"
        }
    }