I have two collections parent and child, and both have reference to a third collection namely genetics. I am trying to achieve the following results. Child inherits parent genetics via reference in aggregation pipeline, but if child’s own specific traits are added then a new genetic document is created. I will provide examples to elaborate my case
Parent → Genetics → hair, health, weight
Child → Genetics → intelligence
Now combining parent genetics with child genetics will result in the the following final results
Child traits → hair, health, weight, intelligence
Now suppose that a child has its own similar attribute which is different to parent lets say baldness then child will be like this
Child traits → baldness, health, weight, intelligence
Essentially overriding parent genetic wrt any attribute
I am not saving the results rather I am using aggregation pipeline to show a child’s traits, basically I want parent genetics to override once we get to child traits and if new child trait is added even one attribute then it creates a new document in genetics with child id reference but in aggregation pipeline I will lookup genetic reference in both parent and child while applying aggregation pipeline on child document and merge both parent and child genetics where child traits are either added or overrides parents traits. I am looking for dynamic merging of both parent and child in either $project or $group stage.