title: C# MongoDb - How to update one field in nested documents?
body: <p>What I want to achieve is to provide <code>List&lt;JobList&gt;</code> containing client Id and InternalIds of jobs to the <code>Finish()</code> method.
  Then I want to iterate through clients and update all jobs matches JobInternalIds and set current datetime to FinishedAt field.
  The problem is that I totally don't know how to update nested objects.</p>
  
  <p>I've tried something like below but without success.</p>
  
  <pre><code>public class Client
  {
      [BsonId]
      public ObjectId Id { get; set;}
      public string Name { get; set;}
      public List&lt;Job&gt; Jobs { get; set;}
  }
  
  public class Job
  {
      public string InternalId { get; set;}
      public string Name { get; set;}
      public DateTime? FinishedAt { get; set;}
  }
  
  public class JobList
  {
      public string ClientId { get; set; }
      List&lt;string&gt; JobInternalIds { get; set; }
  }
  
  public async Task Finish(List&lt;JobList&gt; joblist)
  {   
      var updateDefinition = new UpdateDefinitionBuilder&lt;Client&gt;()
          .Set(x =&gt; x.Job[0].FinishedAt, DateTime.UtcNow); // don't know how to set datetime to finishedAt for each elements of collection that matches InternalIds
  
      foreach(var item in joblist)
      {
          await _db.Collection.UpdateManyAsync(item.ClientId, updateDefinition);
      }   
  }
  
  ----------EDITED----------
  public async Task Finish(List&lt;JobList&gt; joblist)
  {
      var updateDefinition = Builders&lt;Client&gt;.Update
          .Set(x =&gt; x.Jobs[-1].FinishedAt, DateTime.UtcNow);
  
      foreach (var item in joblist)
      {
          var internalIds = item.JobInternalIds.Select(x =&gt; x.InternalId).ToList();   
          var idFilter = Builders&lt;Client&gt;.Filter.Eq(x =&gt; x.Id, item.ClientId);
          var internalIdsFilter = Builders&lt;Client&gt;.Filter.ElemMatch(x =&gt; x.Jobs, y =&gt; internalIds.Contains(y.InternalId));
          var combinedFilters = Builders&lt;Client&gt;.Filter.And(idFilter, internalIdsFilter);
  
          await _db.Collection.UpdateManyAsync(combinedFilters, updateDefinition);
      }
  }
  </code></pre>