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.
{
"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"
}
}
]
}
]
}
I tried using this:
var filter = Builders<Project>.Filter.And(Builders<Project>.Filter.Eq(x => x.ProjectID, projectID), Builders<Project>.Filter.ElemMatch(x => x.ProjectTasks, x => x.ProjectTaskID == projectTaskID), Builders<Project>.Filter.ElemMatch(x => x.ProjectTasks.ElementAt(-1).Comments, x => x.CommentID == comment.CommentID));
var update = Builders<Project>.Update.Set(x => x.ProjectTasks.ElementAt(-1).Comments.ElementAt(-1).CommentDescription, comment.CommentDescription );
Collection.UpdateOneAsync(filter, update, new UpdateOptions() { IsUpsert = true });
I also tried to change the filter to
var filter = Builders<Project>.Filter.And(Builders<Project>.Filter.Where(p => p.ProjectID == projectID), Builders<Project>.Filter.Eq("ProjectTasks.ProjectTaskID", projectTaskID), Builders<Project>.Filter.Eq("ProjectTasks.$.Comments.$.CommentID", comment.CommentID));
For both cases, I'm not able to query, filter and update the comments.
Can you please tell me how to find and update the comments in this document? Any suggestion is much appreciated!