Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update field in array mongodb c# driver

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.

like image 638
Vladyslav Furdak Avatar asked Aug 04 '15 09:08

Vladyslav Furdak


1 Answers

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.

like image 100
FireAlkazar Avatar answered Sep 17 '22 22:09

FireAlkazar