title: How to deal with references in azure cosmos db
body: <p>First of all I'm new to CosmosDB (NoSQL) and have only worked with SQL-Server so far. I want to migrate to CosmosDB and have a hard time understanding some basic concepts. I understand that everything is a document and there are no joins, everything is denormalized and so on.</p>
  
  <p>Let's say I want to create a model for some sports league. I have teams, teams have players, and the teams are in a league. </p>
  
  <pre><code>{
  "typename": "League",
  "id": "league.1",
  "teams": [
      {
          "typename": "Team",
          "id": "team.1",
          "leagueId": "league.1",
          "players": [
              {
                  "typename": "Player",
                  "id": "Player.1",
                  "teamId": "team.1",
                  "name": "John"
              },
              {
                  "typename": "Player",
                  "id": "Player.1",
                  "teamId": "team.1",
                  "name": "Alex"
              }
          ]
      }
  ]
  </code></pre>
  
  <p>}</p>
  
  <p>The players and teams don't change very often so (I think) it would be fine to store them in a league document. If I would query for a specific league I would get all teams and all players. What if I would query for a specific team? Or what if I want to get the team a specific player is in? Do I have to create documents for every team / player?</p>
  
  <p>What if I would create a new player document? From my actual understanding I would have to update the team that contains the player and even the league. Is this the right approach? This somehow doesn't seem natural to me. If so, how would I do this? I tried stored procedures or triggers. Both are fairly hard to implement (my opinion) as I have no tools to test and debug. The triggers also would be executed recursive (create player leads to updating the team, which leads to updating the league).</p>
  
  <p>If this is the way to go, what is best practice here? Stored procedures? Triggers? Are there any tools to make the development more productive?</p>
  
  <p>I also tried referencing the teams in the league by their id. This makes updating the teams really easy. The downside is that I would have several calls to get all teams of a given league resulting in higher read times.</p>
  
  <p>Can someone get me on the right track?</p>