2016-04-21 88 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);