简体   繁体   English

如何使用mongo c#driver更新数组/列表中的项目?

[英]how to update items in an array/list with mongo c# driver?

With the recent update to 10gen c# driver for mongodb I want to update my code so it use the strongly typed version. 随着mongodb的10gen c#驱动程序的最新更新,我想更新我的代码,因此它使用强类型版本。

My previous call was : 我之前的电话是:

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

The new call would be : 新的电话会是:

update.Set(x => x.Ratings[index].Number, number);
//update2.Set("Ratings." + index + ".Number", number); previous call

But I get this exception : 但是我得到了这个例外:

Unable to determine the serialization information for the expression: (Album x) => x.Ratings.get_Item(WebApp.Areas.API.Controllers.RatingController+<>c__DisplayClass5.index).Number. 无法确定表达式的序列化信息:(Album x)=> x.Ratings.get_Item(WebApp.Areas.API.Controllers.RatingController + <> c__DisplayClass5.index).Number。

Is there any way I can update an item inside a List? 有什么方法可以更新List中的项目吗?

Interesting problem. 有趣的问题。 This works when using a constant like below: 这在使用如下常量时有效:

var update = Update<Album>.Set(x => x.Ratings[0].Number, 10);

However, this apparently breaks when you use a variable, like you have done with index. 但是,当您使用变量时,这显然会中断,就像您使用索引一样。 This is definitely a bug. 这绝对是个错误。 I have created a Jira issue for it here: https://jira.mongodb.org/browse/CSHARP-598 . 我在这里创建了一个Jira问题: https//jira.mongodb.org/browse/CSHARP-598

This is most likely due to us not partially evaluating the expression before processing it. 这很可能是因为我们在处理表达式之前没有对其进行部分评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM