Search code examples
mongodbmongodb-java

Set item in array using com.mongodb.client.model.Updates.set


I try to update an array element using MongoDB Java driver API method com.mongodb.client.model.Updates.set like this:

set(String.format("bag.items[%d]", idx), old.map(mo -> mo.frozen).orElse(null))

But instead of updating, the Item it creates a new field with the name "items[0]"

enter image description here


Solution

  • the correct syntax is

    set(String.format("bag.items.%d", idx), old.map(mo -> mo.frozen).orElse(null))