Apply the $set operator together with the $ positional operator in your update to change the name field.
The $ positional operator will identify the correct element in the array to update without explicitly specifying the position of the element in the array, thus your final update statement should look like:
db.collection.update(
{ "friends.u.username": "michael" },
{ "$set": { "friends.$.u.name": "hello" } }
)
To update multiple elements in an array when more than one element matches the filter, you can use the $\[<identifier>\] syntax with the arrayFilters option. The following will update all matching elements in the “friends” array:
db.collection.update(
{ "friends.u.username": "michael" },
{ "$set": { "friends.$[elem].u.name": "hello" } },
{
"arrayFilters": [{ "elem.u.username": "michael" }],
"multi": true
}
)
The $[elem] syntax identifies array elements that match the specified filter in the arrayFilters option. The multi: true option is added to update all matching documents instead of just the first one.