Update field in array mongodb c# driver

杀马特。学长 韩版系。学妹 提交于 2019-12-23 10:19:10

问题


I try to update status field for object from p2l array

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 ? 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.


回答1:


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.
I found this solution by exploring UpdateDefinitionBuilderTests.



来源:https://stackoverflow.com/questions/31804925/update-field-in-array-mongodb-c-sharp-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!