5

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.

Community
  • 1
  • 1
Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46
  • You do realize "why" this is notated in this way and therefore not a function applied to array members as with a lambda don't you? Two different things. The purpose here is to "match" the array element in the "query" expression of the update. Then the "matched index" is notated in this way so the correct element is updated. – Blakes Seven Aug 04 '15 at 09:04

1 Answers1

9

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.

FireAlkazar
  • 1,795
  • 1
  • 14
  • 27