Search code examples
.netmongodbmongodb-.net-driver

mongodb .NET driver update nested array


I am trying to increase a value in a nested array accessing by index. To make sense, Client object contains a list of Order object. Order object contains int Stars. My goal is to increase Stars int by one like:

            await mongoContext.Clients.UpdateOneAsync(x => x.Id == model.PostId,
           Builders<Client>.Update.Inc(x => x.Orders.ElementAt(index).Stars, 1));

Code above gives me error message: enter image description here If I pass an exact number (not variable), it works fine:

            await mongoContext.Clients.UpdateOneAsync(x => x.Id == model.PostId,
           Builders<Client>.Update.Inc(x => x.Orders.ElementAt(0).Stars, 1));

I have also tried the simpler approach x => x.Orders[index].Stars (not through "ElementAt()") but it keeps failing.

Any help is welcome.


Solution

  • This is a known issue, please check this

    It is solved in driver versions later to 2.0.0 (2.6.0 is the current one).