使用C#在MongoDB中查询,过滤和更新多级嵌套数组

时间:2016-01-27 14:59:41

标签: c# mongodb mongodb-csharp-2.0

我有这个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));

对于这两种情况,我无法查询,过滤和更新评论。

您能告诉我如何查找和更新本文档中的评论吗?任何建议都非常感谢!

2 个答案:

答案 0 :(得分:3)

我遇到了类似的问题 - 我需要在嵌套集合中插入一个新项目。假设我们无法更改数据结构,我们的想法是找到要更新的嵌套实体的索引。然后,可以在更新定义中访问该实体。

在给定的上下文中,我所做的如下:

var projectTaskIndex = await _mongoContext.Projects
    .Find(p => p.ProjectID == projectID)
    .Project(p => p.ProjectTasks.FindIndex(t => t.ProjectTaskID == projectTaskID))
    .SingleOrDefaultAsync();

var updateDefinition = new UpdateDefinitionBuilder<Project>()
    .AddToSet(p => p.ProjectTasks[projectTaskIndex].Comments, comment);

await _mongoContext.Projects
    .UpdateOneAsync(p=> p.ProjectID == projectID, updateDefinition);

以同样的方式,可以找到评论的索引并访问评论的属性。更新定义如下所示:

var updateDefinition = new UpdateDefinitionBuilder<Project>()
    .Set(p => p.ProjectTasks[projectTaskIndex]
                 .Comments[commentIndex].CommentDescription,
            comment.CommentDescription);

这里非常重要的是确保线程安全 - 在获取索引和实际更新之间,ProjectTasks和Comments的集合不得更改。

@Cisco,您找到了解决方案吗?如果是的话,请你分享一下吗?

答案 1 :(得分:1)

您应该使用$ []多位置运算符,我将尝试为粘贴的代码编写内容:

var baseFilter = Builders<Project>.Filter.Eq("ProjectID": 1);
var update = Builders<Project>.Update.Set("ProjectTasks.$[i].Comments.$[j].CommentDescription", comment.CommentDescription);

var arrayFilters = new List<ArrayFilterDefinition>
{
    /* change the type names here if they have different names, I just guessed */
    new BsonDocumentArrayFilterDefinition<ProjectTask>(new BsonDocument("i.ProjectTaskID", projectTaskID)),
    new BsonDocumentArrayFilterDefinition<Comment>(new BsonDocument("j.CommentId", commentID))
};

var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };

await Collection.UpdateOneAsync(baseFilter, update, updateOptions);