2016-04-21 84 views
0

该代码失败:最佳的方式来更新嵌套数组在C#/ MongoDB的

public async Task InsertFile(string projectId, string folderId, File file) 
    { 
     var project = await Find(projectId); 
     var folderIndex = project.Folders.IndexOf(project.Folders.Single(x => x.Id == folderId)); 
     var update = Builders<Project>.Update.Push(x => x.Folders[folderIndex].Files, file); 
     await Collection().UpdateOneAsync(x => x.Id == projectId, update); 
    } 

给出的错误是:

Unable to determine the serialization information for x => x.Folders.get_Item(value(Launch.Business.Database.ProjectStore+<>c__DisplayClass3_0).folderIndex).Files. 

所以我不能索引对象嵌套数组。

我需要原子更新才能工作。我无法保存整个项目,因为文件夹更新非常迅速,可能会相互冲突/互相覆盖。

有了这个约束,什么是最短和最甜蜜的方式来实现上述?

回答

0

我发现,上面的代码可以工作,但它不适用于表达式语法。

与串更换表达式语法修复它:

var update = Builders<Project>.Update.Push($"Folders.{folderIndex}.Files", file);