{
field:"value",
facilityId:"H001",
alternativeId:["deafaultID#12312-213-1314"],
}
I want to replace all the document "defaultID" from the array of alternativeId and change to "NEWID" only if the facilityId is "H001"
facilityId: "H001" condition$map to iterate loop of alternativeId array$replaceOne to replace find string by replacementdb.collection.update(
{ facilityId: "H001" },
[{
$set: {
alternativeId: {
$map: {
input: "$alternativeId",
in: {
$replaceOne: {
input: "$$this",
find: "deafaultID",
replacement: "NEWID"
}
}
}
}
}
}],
{ multi: true }
)
$replaceOneand$mapoperators.