更新数组mongodb C#驱动程序中的字段 [英] Update field in array mongodb c# driver
问题描述
我尝试从p2l
数组更新对象的status
字段
I try to update status
field for object from p2l
array
var update = Builders<BsonDocument>.Update.Set("p2l.$.status",BsonValue.Create(status))
似乎代码可以正常工作,但是如何使用类型化的构建器实现它并使用lambda设置所有字段? 我通过以下链接找到了解决方案 如何更新使用C#驱动程序包含在MongoDB中数组子文档中的数组子文档中的字段?
It seems that code will work fine, but how to implement it with typed builder and set all fields with lambda ? I found a solution by the following link How to update a field in an array's subdocument contained in an array's subdocument in MongoDB using C# driver?
但是它仅适用于旧版本的驱动程序.
But it suitable only for old version of driver.
推荐答案
您可以尝试以下方法:
Builders<Person>.Update.Set(x => x.Pets[-1].Name, "Fluffencutters")
注意Pets集合上的 -1 索引,这意味着将集合应用于所有元素.
我通过探索 UpdateDefinitionBuilderTests 找到了这个解决方案.
Note -1 index on Pets collection, that means to apply set for all elements.
I found this solution by exploring UpdateDefinitionBuilderTests.
这篇关于更新数组mongodb C#驱动程序中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!