我有这个 MongoDB 文档。我正在开发一个 MVC 应用程序并尝试使用 C# 更新评论数组(评论描述为“更新后评论”)。我正在使用新的 mongodb 版本。
{
"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"
}
}
]
}
]
}
我尝试使用这个:
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 });
我也尝试将过滤器更改为
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));
对于这两种情况,我都无法查询、过滤和更新评论。
您能告诉我如何查找和更新本文档中的评论吗?任何建议都非常感谢!