Dynamic update you can use update with aggregation pipeline starting from MongoDB 4.2,
First approach: using $rpelaceOne
,
$objectToArray
convert data.values
from object to array in k and v format
$map
to iterate loop of above converted array
$replaceOne
will replace string on the base of input and replacement
$arrayToObject
back to convert from array to object
db.collection.update({},
[{
$set: {
"data.values": {
$arrayToObject: {
$map: {
input: { $objectToArray: "$data.values" },
in: {
k: {
$replaceOne: {
input: "$$this.k",
find: "Avg",
replacement: "MT"
}
},
v: "$$this.v"
}
}
}
}
}
}
])
Playground
Second approach: using $split
, $arrayElemAt
, $concat
,
Playground
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…