6
隨着最近更新到10gen c# driver for mongodb我想更新我的代碼,所以它使用強類型的版本。如何使用mongo c#驅動程序更新數組/列表中的項目?
我以前的電話是:
var update2 = new UpdateBuilder();
var index = album.Ratings.IndexOf(rating);
update2.Set("Ratings." + index + ".Number", number);
update2.Set("Rating", album.Rating);
_session.Db().GetCollection<Album>("Album")
.Update(Query<Album>.Where(x => x.Id == objId), update2); //this line is working
新的呼叫將是:
update.Set(x => x.Ratings[index].Number, number);
//update2.Set("Ratings." + index + ".Number", number); previous call
但我得到這個異常:
無法確定表達式的序列化信息: (Album x)=> x.Ratings.get_Item(WebApp.Areas.API.Controll ers.RatingController + <> c__DisplayClass5.index).Number。
有什麼辦法可以更新列表中的項目嗎?
感謝Craig,當我調用_session.Db()。GetCollection(「Advertisement」)的時候,如果我們不必寫「Advertisement」並且它默認使用名稱的類,所以我們不會有應用程序中的魔術字符串!這是在mongo-LINQ中完成的一些非常好的工作! –
VinnyG
這是一個正在進行的辯論。 https://jira.mongodb.org/browse/CSHARP-126。目前,大多數人編寫一個使用typeof(T).Name作爲集合名稱的擴展方法。 –
自1.6.1起固定 – VinnyG