title: How to aggregate distinct items in mongo db to separate array?
body: <p>I have an API endpoint that checks the mongo db and returns in a following structure for example:</p>
  <pre><code>{
    &quot;Name&quot;: &quot;Test&quot;
    &quot;Location&quot;: &quot;Whatever&quot;,
    &quot;customerServices&quot;: [
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;serviceAdr&quot;: &quot;test&quot;,
          &quot;serviceCounty&quot;: &quot;West&quot;,
          &quot;name&quot;: &quot;test&quot;
        },
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;serviceAdr&quot;: &quot;test&quot;,
          &quot;serviceCounty&quot;: &quot;West&quot;,
          &quot;name&quot;: &quot;test&quot;
        },
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;serviceAdr&quot;: &quot;test&quot;,
          &quot;serviceCounty&quot;: &quot;East&quot;,
          &quot;name&quot;: &quot;test&quot;
        }
    ]
  }
  </code></pre>
  <p>What I want to do is to add aggregation to the pipeline that would <strong>create additional field-serviceCounties</strong> and include <strong>distinct</strong> serviceCounty values from the customerServices array. Like the following:</p>
  <pre><code>{
    &quot;Name&quot;: &quot;Test&quot;
    &quot;Location&quot;: &quot;Whatever&quot;,
    &quot;customerServices&quot;: [
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;serviceAdr&quot;: &quot;test&quot;,
          &quot;serviceCounty&quot;: &quot;West&quot;,
          &quot;name&quot;: &quot;test&quot;
        },
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;serviceAdr&quot;: &quot;test&quot;,
          &quot;serviceCounty&quot;: &quot;West&quot;,
          &quot;name&quot;: &quot;test&quot;
        },
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;serviceAdr&quot;: &quot;test&quot;,
          &quot;serviceCounty&quot;: &quot;East&quot;,
          &quot;name&quot;: &quot;test&quot;
        }
    ],
    &quot;serviceCounties&quot;: [
      {
        &quot;countyName&quot;:&quot;East&quot;
      },
      {
        &quot;countyName&quot;:&quot;West&quot;
      }
  ] 
  }
  </code></pre>
  <p>I have tried the following, but it didn't work. There something I'm missing or doing wrong:</p>
  <pre><code>                'serviceCounties': {
                      '$reduce': {
                          'input': '$services.event.services',
                          'initialValue': [],
                          'in': [{
                              &quot;countyName&quot;: { '$concatArrays': [&quot;$$value.serviceCounty&quot;, &quot;$$this&quot;] }
                          }]
                      }
                  }
  </code></pre>
  <p><strong>Any ideas how to do it ?</strong></p>