【发布时间】:2017-08-31 17:56:39
【问题描述】:
我正在尝试更新文档中数组中特定对象的属性。
例如:
{
_id: #####
Items: [
{ Key: 1, Value: "Something" },
{ Key: 2, Value: "Foo" },
{ Key: 1, Value: "Bar" },
]
}
我正在使用 MongoDB C# 2.0 驱动程序,这就是我的过滤器(尽管我很确定这将匹配整个文档,而不是子文档)。
FilterDefinition<GroupDto> filter = Builders<GroupDto>.Filter.Eq(i => i.Id, groupId) &
Builders<GroupDto>.Filter.ElemMatch(i => i.Items, u => u.Key == key);
实际上,我想要实现的是按 Id 匹配文档,然后在 Items 数组中找到“Key”匹配的对象,然后仅更新该特定数组对象的“Value”属性。所以我匹配 Key: 2,我可以只更新 Key: 2 的“Value”字段,并且 Key: 1 和 Key: 3 保持不变。
这可能吗?
干杯, 贾斯汀
【问题讨论】:
-
是的,这个问题就是我要找的……我不知道这个词是位置运算符。所以我不能将 Linq 用于 $ 位置值。干杯。