Setting the scene:-
I am writing functions to update fields within a collection based on values in multiple separate collections. Most of the updates are of the form if fieldA is null lookup a value from table 2. set fieldA = looked_up_value_from_table2
To do this I am using an aggregation pipeline followed by a merge.
Problem.
While the merge is setting the right value it is also overwriting some data I expected it to be leaving. E.g If I merge value ‘content.labels.name’ in the json copied below, the whole of content branch is overwritten.
{
“_id” : ObjectId(“5b0e9ca36f72c14f486e4e4d”),
“team_id” : 28,
“service_name” : “aaa”,
“updated_at” : ISODate(“2019-05-30T00:00:00.000Z”),
“created_at” : ISODate(“2018-05-30T00:00:00.000Z”),
“document_type” : “questionnaireResponse”,
“content” : {
“questionnaire_uuid” : “9bf3daa775d3d79758f10186679aaf1baccb3eb219176d69ac84c15e77e9d3e3”,
“questionnaire_name” : “B1234”,
“status” : “expired”,
“labels” : {
“referral_uuid” : “99999”,
“context” : “rrh_as”
}
},
“basfiFound” : true
}
merge with values for _id and ‘content.labels.name’ results in
{
“_id” : ObjectId(“5b0e9ca36f72c14f486e4e4d”),
“team_id” : 28,
“service_name” : “aaa”,
“updated_at” : ISODate(“2019-05-30T00:00:00.000Z”),
“created_at” : ISODate(“2018-05-30T00:00:00.000Z”),
“document_type” : “questionnaireResponse”,
“content” : {
“labels” : {
“name” : “new value”
}
},
“basfiFound” : true
}
I lose the whole of the content branch
Is there a way to get merge to not overwrite the parts of the branch not being updated? … or is there a better way to do this not using merge?