I have a documents as follows:
Document 1:
{
"ARCM Sum" : {
"MCA Go" : "60",
"Assess" : "5030",
"ActivitID" : "467144"
"Activity Checks" : [
{
"Frequency within Activity Description matches Activity Frequency" : "Exception",
"Activity Description contains > 20 word" : "Pass"
}
]
"Week" : "04-05-2021",
"Load Datetime" : "07-14-2021"
}
}
Document 2:
{
"ARCM Sum" : {
"MCA Go" : "60",
"Assess" : "5030",
"ActivitID" : "467144"
"Activity Checks" : [
{
"Frequency within Activity Description matches Activity Frequency" : "Exception",
"Activity Description contains > 20 words" : "Pass"
}
]
"Week" : "04-05-2021",
"Load Datetime" : "07-14-2021"
}
}
I need to update document from "word" to "words" in "Activity Description contains > 20 word" , so
for example in Document 1 there is "Activity Description contains > 20 word" need to update to "Activity Description contains > 20 words"
Document 2 will be ignored as it already has words in "Activity Description contains > 20 words"
CodePudding user response:
Usually you would use $rename
but because it is a field in an array you can't do it directly. Here you could use an update with an aggregation pipeline, that $maps the relevant documents:
This is available since MongoDB 4.2 Update with aggregation pipeline
db.test.update(
{ "ARCM Sum.Activity Checks.Activity Description contains > 20 word": {"$exists": true} },
[{
"$set": {
"ARCM Sum.Activity Checks": {
"$map": {
"input": "$ARCM Sum.Activity Checks",
"in": {
"Frequency within Activity Description matches Activity Frequency": "$$this.Frequency within Activity Description matches Activity Frequency",
"Activity Description contains > 20 words": "$$this.Activity Description contains > 20 word"
}
}
}
}
}],
{ multi: true }
)