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.

1
  • 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. Commented Aug 4, 2015 at 9:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.