Hello @Dan_Burt, you can use the Updates with Aggregation Pipeline feature to update different values of holeGross for all the elements of the holes array.
Suppose you have a document with two holes, for example:
{
"_id" : 1,
"player" : "John",
"holes" : [
{
"no" : 1,
"par" : 3,
"holeGross" : 2
},
{
"no" : 2,
"par" : 4,
"holeGross" : 3
}
]
}
and the new values of hole gross in an array, for example:
var new_vals = [ 9, 5 ]
The following update operation will change each element of the array with new values (in your case, you need to supply an array of 8 elements as there are eight holes) in a single call.
db.collection.updateOne(
{ _id: 1 },
[
{
$set: {
holes: {
$map: {
input: { $range: [ 0, { $size: "$holes" } ] },
in: {
$mergeObjects: [
{ $arrayElemAt: [ "$holes", "$$this" ] },
{ holeGross: { $arrayElemAt: [ new_vals, "$$this" ] } }
]
}
}
}
}
}
]
)