'Flatten mongo db field

How can I flatten "Behavior" field so it becomes part of the document root ? I have tried with $project and $unwind functions but that ones did not seem to help me

I have json data like this:

  "breed": "Akita",
  "origin": "Japan",
  "url": "https://en.wikipedia.org/wiki/Akita_(dog)",
  "img": "some img url",
  "WikiDescr": [
    {
      "contenido": "Content"
    }
  ],
  "Behavior": [
    {
      "good_with_children": 3,
      "good_with_other_dogs": 1,
      "shedding": 3,
      "grooming": 3,
      "drooling": 1,
      "coat_length": 1,
      "good_with_strangers": 2,
      "playfulness": 3,
      "protectiveness": 5,
      "trainability": 3,
      "energy": 4,
      "barking": 2,
      "min_life_expectancy": 10,
      "max_life_expectancy": 14,
      "max_height_male": 28,
      "max_height_female": 28,
      "max_weight_male": 130,
      "max_weight_female": 100,
      "min_height_male": 26,
      "min_height_female": 26,
      "min_weight_male": 100,
      "min_weight_female": 70
    }

and my desired output would be like this:

      "breed": "Akita",
      "origin": "Japan",
      "url": "https://en.wikipedia.org/wiki/Akita_(dog)",
      "img": "some img url",
      "good_with_children": 3,
      "good_with_other_dogs": 1,
      "shedding": 3,
      "grooming": 3,
      "drooling": 1,
      "coat_length": 1,
      "good_with_strangers": 2,
      "playfulness": 3,
      "protectiveness": 5,
      "trainability": 3,
      "energy": 4,
      "barking": 2,
      "min_life_expectancy": 10,
      "max_life_expectancy": 14,
      "max_height_male": 28,
      "max_height_female": 28,
      "max_weight_male": 130,
      "max_weight_female": 100,
      "min_height_male": 26,
      "min_height_female": 26,
      "min_weight_male": 100,
      "min_weight_female": 70
},
 "WikiDescr": [
    {
      "contenido": "content"
    }
  ],

I have tried this but it does not happen anything:

  { $unwind: "$Behavior" },
  { $replaceRoot: { newRoot: { $mergeObjects: ["$Behavior", "$$ROOT"] } } },

Any help would be appreciated since I am stuck with this trivial question



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source