Merge objects in projections is not working correctly for deep populate

Hey there,

I have a problem with merging objects to the root level while using deep population.
My schema looks like that:

[
  {
    title: {
      de: "Test",
      en: "test"
    },
    infos: [
      {
        label: {
          de: "Test",
          en: "test"
        },
        data: [
          {
            label: {
              de: "Test",
              en: "test"
            }
          },
          
        ],
        
      }
    ],
    
  }
]

When using the aggregation function everything working fine with this syntax. I’m getting the following response when I execute:

      title: `$title.${languageCode}`,
      description: `$description.${languageCode}`,
      infos: {
        $map: {
          input: '$infos',
          in: {
            $mergeObjects: [
              '$$this',
              {
                label: `$$this.label.${languageCode}`,
              },
              {
                data: {
                  $map: {
                    input: '$$this.data',
                    as: 'data',
                    in: {
                      $mergeObjects: [
                        '$$data',
                        {
                          label: `$$data.label.${languageCode}`,
                        },
                        {
                          description: `$$data.description.${languageCode}`,
                        },
                      ],
                    },
                  },
                },
              },
            ],
          },
        },
      },
    }```

The response looks as expected like that:

title 'Test'
....

So I merge the right language and only display that string.

Now to my actual problem. When I'm using deep population, so:


  {
    path: 'exercises.exercise',
    select: ExerciseProtection.DEFAULT(languageCode),
    populate: ExercisePopulate.DEFAULT(languageCode), -> includes the projection showing above
  },
The "infos" were merged correctly but title and description are not anymore in the results.
If I change the projection: title: '$title.en' to someOtherTile: '$title.en' the "someOtherTitle" is displayed in the results.

Does someone has an idea why when I use title it is not displayed in the results but when using someOtherTitle it is in the results?

Thanks!
1 Like

Hi @Tobias_Duelli, welcome to the community.
Can you please mention the reason behind this configuration:

And what is the value of the ExerciseProtection.DEFAULT(languageCode) variable?

The select option in your populate configuration will limit the results to just contain the fields that are mentioned there. I’d recommend you to just remove the select option from your config, validate the output of the populate command and then you can start mentioning the keys in the configuration.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.