title: How to create and update a relation between documents in DocumentDB using ASP.NET Web API
body: <p>I am new to .NET and Azure, and am trying to create a simple Web API, to help me learn. I have two collections of DocumentDB documents. The documents in each collection are defined as follows:</p>
  
  <pre><code>public class Log
      {
          [JsonProperty(PropertyName = "id")]
          public string Id { get; set; }
  
          [JsonProperty(PropertyName = "studentName")]
          public string StudentName { get; set; }
  
          [JsonProperty(PropertyName = "assignment")]
          public string Assignment { get; set; }
  
  
          [JsonProperty(PropertyName = "dueDate")]
          public DateTime DueDate { get; set; }
  
          [JsonProperty(PropertyName = "goal")]
          public string Goal { get; set; }
  
          [JsonProperty(PropertyName = "reflection")]
          public string Reflection { get; set; }
  
          [JsonProperty(PropertyName = "sessions")]
          public List&lt;Session&gt; Sessions { get; set; }
      }
  
      public class Session
      {
          [JsonProperty(PropertyName = "id")]
          public string Id { get; set; }
  
          [JsonProperty(PropertyName = "date")]
          public DateTime Date { get; set; }
  
          [JsonProperty(PropertyName = "duration")]
          public TimeSpan Duration { get; set; }
  
          [JsonProperty(PropertyName = "comment")]
          public string Comment { get; set; }
      }
  </code></pre>
  
  <p>Here is an example of what I have for the Log documents:</p>
  
  <pre><code>{
      "id": "2",
      "studentName": "Joe student",
      "assignment": "Web APIs",
      "dueDate": "0001-01-01T00:00:00",
      "goal": "Keep mistakes to a minimum.",
      "reflection": "I'm getting there.",
      "sessions": [ ]
  }
  </code></pre>
  
  <p>Here is an example of what I have for the Session documents:</p>
  
  <pre><code>{
      "id": "7",
      "date": "2015-04-26T00:00:00",
      "duration": "00:30:00",
      "comment": "Practiced for 30 minutes"
  }
  </code></pre>
  
  <p>I would like to relate Sessions with Logs such that each Log may have several sessions, but each session will be related to a single Log, and also a single Student. I think I want the sessions as separate documents, because I may want to get a list of Sessions by either Student, or Log. There would be Student documents as well.
  I am struggling to add Sessions to a Log, without simply duplicating the session within the log. I would Like to get something like this, which lists the id's of related sessions:</p>
  
  <pre><code>{
      "id": "2",
      "studentName": "Joe student",
      "assignment": "Web APIs",
      "dueDate": "0001-01-01T00:00:00",
      "goal": "Keep mistakes to a minimum.",
      "reflection": "I'm getting there.",
      "sessions": [1, 2, 7, 19, 22]
  }
  </code></pre>
  
  <p>I would then like to display the log, by replacing the ids with the content of the actual documents.</p>
  
  <p>Here are some related working code snippets ( Not necessarily correct) to show you my design structure:</p>
  
  <pre><code>public class PracticeLogRepository : DocumentDB
  {
      // ********** Logs section ********** //
      private PracticeSessionRepository _sessionsRepository;
  
      // specifies the database and document collection used by the repository
      public PracticeLogRepository() : base("Project3", "Logs") { }
  
      // Gets a  list of practice Logs
      public Task&lt;List&lt;Practice.Log&gt;&gt; GetLogsAsync()
      {
          return Task&lt;List&lt;Practice.Log&gt;&gt;.Run(() =&gt; Client
              .CreateDocumentQuery&lt;Practice.Log&gt;(Collection.DocumentsLink)
              .ToList());
      }
  
      // Gets the practice Log with the matching id
      public Task&lt;Practice.Log&gt; GetLogAsync(string id)
      {
          return Task&lt;Practice.Log&gt;.Run(() =&gt; Client
              .CreateDocumentQuery&lt;Practice.Log&gt;(Collection.DocumentsLink)
              .Where(pl =&gt; pl.Id == id)
              .AsEnumerable()
              .FirstOrDefault());
      }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public class PracticeSessionRepository : DocumentDB
  {
      // ********** Session section ********** //
  
      // specifies the database and document collection used by the repository
      public PracticeSessionRepository() : base("Project3", "Sessions") { }
  
      // Gets a  list of practice Sessions
      public Task&lt;List&lt;Practice.Session&gt;&gt; GetSessionsAsync()
      {
          return Task&lt;List&lt;Practice.Session&gt;&gt;.Run(() =&gt; Client
              .CreateDocumentQuery&lt;Practice.Session&gt;(Collection.DocumentsLink)
              .ToList());
      }
  
      // Gets the practice Log with the matching id
      public Task&lt;Practice.Session&gt; GetSessionAsync(string id)
      {
          return Task&lt;Practice.Session&gt;.Run(() =&gt; Client
              .CreateDocumentQuery&lt;Practice.Session&gt;(Collection.DocumentsLink)
              .Where(pl =&gt; pl.Id == id)
              .AsEnumerable()
              .FirstOrDefault());
      }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public class LogController : ApiController
  {
      private PracticeLogRepository _logsRepository;
  
      public LogController()
      {
          _logsRepository = new PracticeLogRepository();
      }
  
      // GET: api/Log
      public async Task&lt;IHttpActionResult&gt; Get()
      {
          var logs = await _logsRepository.GetLogsAsync();
          if (logs != null)
              return Ok(logs);
          return NotFound();
      }
  
      // Get: api/Log/{id}
      public async Task&lt;IHttpActionResult&gt; Get(string id)
      {
          var log = await _logsRepository.GetLogAsync(id);
          if (log != null)
              return Ok(log);
          return NotFound();
      }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public class SessionController : ApiController
  {
      private PracticeSessionRepository _sessionsRepository;
  
      public SessionController()
      {
          _sessionsRepository = new PracticeSessionRepository();
          //_logsRepository = new PracticeLogRepository();
      }
  
      // GET: api/Session
      public async Task&lt;IHttpActionResult&gt; Get()
      {
          var sessions = await _sessionsRepository.GetSessionsAsync();
          if (sessions != null)
              return Ok(sessions);
          return NotFound();
      }
  
      // Get: api/Session/{id}
      public async Task&lt;IHttpActionResult&gt; Get(string id)
      {
          var session = await _sessionsRepository.GetSessionAsync(id);
          if (session != null)
              return Ok(session);
          return NotFound();
      }
  </code></pre>
  
  <p>...</p>
  
  <p>Since I am teaching myself, and am extremely new .NET and C#, and of course DocumentDB, I am struggling to code this. I would really appreciate a simple example of how I should create the Log document, and also how to update it by adding sessions. </p>
  
  <p>If you need to see more of my code, just ask. I just don't want to over complicate this.</p>