[英]Update field in array mongodb c# driver
I try to update status
field for object from p2l
array 我尝试从
p2l
数组更新对象的status
字段
var update = Builders<BsonDocument>.Update.Set("p2l.$.status",BsonValue.Create(status))
It seems that code will work fine, but how to implement it with typed builder and set all fields with lambda ? 似乎代码可以正常工作,但如何使用类型化构建器实现它并使用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?
我通过以下链接找到了解决方案如何使用C#驱动程序更新MongoDB中数组子文档中包含的数组子文档中的字段?
But it suitable only for old version of driver. 但它仅适用于旧版本的驱动程序。
You can try something like: 您可以尝试以下方式:
Builders<Person>.Update.Set(x => x.Pets[-1].Name, "Fluffencutters")
Note -1 index on Pets collection, that means to apply set for all elements. 注意-1 Pets上的索引,表示为所有元素应用set。
I found this solution by exploring UpdateDefinitionBuilderTests . 我通过探索UpdateDefinitionBuilderTests找到了这个解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.