c# - 如何使用 mongo c# 驱动程序更新数组/列表中的项目?

标签 c# mongodb mongodb-.net-driver

最近更新到 10gen c# driver for mongodb我想更新我的代码,使其使用强类型版本。

我之前的电话是:

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

但我得到了这个异常(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.

有什么方法可以更新列表中的项目吗?

最佳答案

有趣的问题。这在使用如下常量时有效:

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

但是,当您使用变量时,这显然会中断,就像您使用索引所做的那样。这绝对是一个错误。我在这里为它创建了一个 Jira 问题:https://jira.mongodb.org/browse/CSHARP-598 .

这很可能是因为我们在处理它之前没有对表达式进行部分评估。

关于c# - 如何使用 mongo c# 驱动程序更新数组/列表中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803563/

相关文章:

c# - Parallel.For 在大约 1370 次迭代后卡住,不知道为什么

C#:如何翻译 Yield 关键字

c# - 设计 View 未在 Visual Studio 2015 中打开

node.js - 创建模型时出现 Mongoose 错误(使用步骤)

java - 无法将聚合查询从 mongodb 转换为 Spring Data MongoDb

c# - 在 mongodb 中,你如何进行纯更新(而不是更新插入)来替换整个类型化的文档?

c# - 将javascript渲染的svg图像作为.png文件保存到本地磁盘

node.js - 在AWS EC2中运行定制的mongo容器

c# - 使用 c# 驱动程序 2.2.3 关闭 mongoDb 服务器

C# MongoDB IAsyncCursor 详解