Hi, this is the first time I have written here for help, I hope to be the more precise I can.
Having the following documents
[{
"_id" : 206,
"Events" : [
{
"_t" : "PageReviewed",
"UpdateDate" : null,
},
{
"_t" : "PageMetadataSet",
"UpdateDate" : null,
"Metadata" : {
"Category" : "Any caSE cateGory",
"Name" : "Any CaSe name",
"Culture" : "*",
"Environment" : "*",
"Platform" : "*",
"Value" : true,
"ValueTypeName" : "System.Boolean"
}
}
]
},
{
"_id" : 207,
"Events" : [
{
"_t" : "PageReviewed",
"UpdateDate" : null,
},
{
"_t" : "PageMetadataSet",
"UpdateDate" : null,
"Metadata" : {
"Category" : "Any casing Category",
"Name" : "Any casing Name",
"Culture" : "*",
"Environment" : "*",
"Platform" : "*",
"Value" : false,
"ValueTypeName" : "System.Boolean"
}
}
]
}]
I would need a way to update the nested property Category and Name inside the Metadata object contained in the Events array.
I’ve tried the updateMany approach with the array element, but I cannot access the original value of the property to be lowercased:
const lowercase = (value) => {
return value.toLowerCase();
}
db.getCollection("vsm.commits.bck").updateMany(
{
"Events._t": /MetadataSet/
},
{
$set : {
"Events.$[m].Metadata.Name": lower("Events.$[m].Metadata.Name")
}
},
{
arrayFilters: [
{"m.Metadata" : {$exists: true}}
]
}
)
Unfortunately, I cannot achieve the desired update since the reference to the item inside the $set to get the original value is not resolved. ($set : { “Events.$[m].Metadata.Name”: lower(“Events.$[m].Metadata.Name”) })
The Name field has been updated with the string specified
"Events.1.Metadata.Name" : "events.$.metadata.name"