title: How to update child document in MongoDB Array using C# Driver
body: <p>How to modify a child element in a child array of a document in MongoDB? I'm using the latest C# Driver for MongoDB (C# Driver Version: <strong>2.5</strong>, Server Version: <strong>3.2.0</strong>) and I've tried solutions from multiple answers on this from SO (duplicate questions also) and from various other sources. Most of the solutions proved to be obsolete.</p>
  
  <p>Below is my sample document:</p>
  
  <pre><code>[{ 
  "_id" : "69c4f77d-05ef-431d-ae17-c076c6173e04",     
  "title" : "Root Page", 
  "createdAt" : ISODate("2017-12-21T12:28:00.680+0000"), 
  "modifiedAt" : ISODate("2017-12-26T07:18:11.165+0000"), 
  "pages" : [
      {
          "_id" : "f449dc0b-3d1b-4a59-b622-6a42ce10b147", 
          "title" : "Test page 1", 
          "slug" : "test-page-1", 
          "createdAt" : ISODate("2017-12-21T12:28:00.680+0000"), 
          "modifiedAt" : ISODate("2017-12-21T12:28:00.680+0000")
      }, 
      {
          "_id" : "3d1497d7-f74c-4d88-b15c-bf2f9c736374", 
          "title" : "Test page 2", 
          "slug" : "test-page-2", 
          "createdAt" : ISODate("2017-12-25T11:27:55.006+0000"), 
          "modifiedAt" : ISODate("2017-12-25T11:27:55.006+0000")
      }, 
      {
          "_id" : "6e827e2a-5a25-4343-b646-885816bb8cc4", 
          "title" : "Test page 3", 
          "slug" : "test-page-3", 
          "createdAt" : ISODate("2017-12-25T11:31:16.516+0000"), 
          "modifiedAt" : ISODate("2017-12-25T11:31:16.516+0000")
      }]
  },
  ...,
  ...
  ]
  </code></pre>
  
  <p>Document Structure:
  The Root Document contains an array of sub documents, which has it's metadata and a list of pages (array)</p>
  
  <p>Inside a Sub Document  - I'm trying to update the <strong>title</strong> and <strong>slug</strong> of a <strong>child page</strong> object inside <strong>pages</strong> array, but have no luck so far with the latest driver. </p>
  
  <p>I'm using the following filter and update query:</p>
  
  <pre><code>        var filter = Builders&lt;SubDocument&gt;.Filter.And(Builders&lt;SubDocument&gt;.Filter.Eq("_id", "xxx"), Builders&lt;SubDocument&gt;.Filter.ElemMatch(x =&gt; x.Pages, p =&gt; p.Id == "xxx"));
  
          var update = Builders&lt;SubDocument&gt;.Update
                          .Set("pages.$.title", "changed title")
                          .Set("pages.$.slug", "changed-title")
                          .Set("pages.$.modifiedAt", DateTime.UtcNow);
  
          _mongoDb.Documents.UpdateOne(filter, update);
  </code></pre>
  
  <p>Now - this query returns me an error:</p>
  
  <blockquote>
    <p>Invalid BSON field name pages.$.title</p>
  </blockquote>
  
  <p>The references I came along for last few hours are saying to use "$" operator but it throws error in latest driver.</p>
  
  <p><strong>[Edit 1]</strong> Here's my models:</p>
  
  <pre><code>public class SubDocument
  {
      public SubDocument()
      {
          Id = Guid.NewGuid().ToString();
          CreatedAt = DateTime.UtcNow;
          ModifiedAt = DateTime.UtcNow;
          Pages = new List&lt;Page&gt;();
      }
  
      [BsonId]
      public string Id { get; set; }        
  
      [BsonElement("title")]
      public string Title { get; set; }        
  
      [BsonElement("createdAt")]
      public DateTime CreatedAt { get; set; }
  
      [BsonElement("modifiedAt")]
      public DateTime ModifiedAt { get; set; }
  
      [BsonElement("pages")]
      public List&lt;Page&gt; Pages { get; set; }
  
  
  }
  
  public class Page
  {
      [BsonId]
      public string Id { get; set; }
  
      [BsonElement("title")]
      public string Title { get; set; }
  
      [BsonElement("slug")]
      public string Slug { get; set; }
  
      [BsonElement("createdAt")]
      public DateTime CreatedAt { get; set; }
  
      [BsonElement("modifiedAt")]
      public DateTime ModifiedAt { get; set; }
  
  }
  </code></pre>
  
  <p>Anyone? Thanks in advance!</p>