Querying, filtering and updating multiple level nested arrays in MongoDB using C#
<p>I have this MongoDB document. I am developing an MVC application and trying to update the comment array (commented description to "comment after update") using C#. I'm using the new mongodb version.</p>

<pre><code>{
   "ProjectID":1,
   "ProjectName":"Project test",
   "ProjectStatus":"Active",
   "ProjectTasks":[
      {
         "ProjectTaskID":1,
         "TaskShortDescription":"short task description",
         "TaskLongDescription":"long task description",
         "Comments":[
            {
               "CommentID":1,
               "CommentDescription":"comment before update",
               "CreatedBy":"Mike",
               "UploadDocuments":{
                  "TaskID":null,
                  "CommentID":null,
                  "UploadDocumentID":1,
                  "UploadDocumentName":"first document upload"
               }
            }
         ]
      }
   ]
}
</code></pre>

<p>I tried using this:</p>

<pre><code>var filter = Builders&lt;Project&gt;.Filter.And(Builders&lt;Project&gt;.Filter.Eq(x =&gt; x.ProjectID, projectID), Builders&lt;Project&gt;.Filter.ElemMatch(x =&gt; x.ProjectTasks, x =&gt; x.ProjectTaskID == projectTaskID), Builders&lt;Project&gt;.Filter.ElemMatch(x =&gt; x.ProjectTasks.ElementAt(-1).Comments, x =&gt; x.CommentID == comment.CommentID));

var update = Builders&lt;Project&gt;.Update.Set(x =&gt; x.ProjectTasks.ElementAt(-1).Comments.ElementAt(-1).CommentDescription, comment.CommentDescription );

Collection.UpdateOneAsync(filter, update, new UpdateOptions() { IsUpsert = true });
</code></pre>

<p>I also tried to change the filter to </p>

<pre><code>var filter = Builders&lt;Project&gt;.Filter.And(Builders&lt;Project&gt;.Filter.Where(p =&gt; p.ProjectID == projectID), Builders&lt;Project&gt;.Filter.Eq("ProjectTasks.ProjectTaskID", projectTaskID), Builders&lt;Project&gt;.Filter.Eq("ProjectTasks.$.Comments.$.CommentID", comment.CommentID));
</code></pre>

<p>For both cases, I'm not able to query, filter and update the comments.</p>

<p>Can you please tell me how to find and update the comments in this document? Any suggestion is much appreciated!</p>