title: How to concatenate multiple string fields into single field using MongoDB Aggregation?
body: <p>Let's suppose I have an incoming data in the following format:</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;adr&quot;: &quot;Adr 1&quot;,
          &quot;serviceCounty&quot;: &quot;Center&quot;,
          &quot;area&quot;: &quot;village&quot;
        },
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;adr&quot;: &quot;adr2&quot;,
          &quot;serviceCounty&quot;: &quot;West&quot;,
          &quot;area&quot;: &quot;city&quot;
        },
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;adr&quot;: &quot;test&quot;,
          &quot;serviceCounty&quot;: &quot;West&quot;,
          &quot;area&quot;: &quot;test&quot;
        }
    ]
  }
  </code></pre>
  <p>Any ideas, how to write an aggregation query which would:</p>
  <ol>
  <li>Create a new field called '<strong>serviceAreas</strong>'. Type: list</li>
  <li>For each item in the 'customerServices'. It would select: <strong>adr</strong>, <strong>serviceCounty</strong> and <strong>area</strong> fields.</li>
  <li>Append them together into a single string and add to the newly created <strong>serviceAreas</strong> field.</li>
  <li>It would select and do the operation only for distinct serviceCounty items</li>
  </ol>
  <p><strong>So the end result would be like this:</strong></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;adr&quot;: &quot;Adr 1&quot;,
          &quot;serviceCounty&quot;: &quot;Center&quot;,
          &quot;area&quot;: &quot;village&quot;
        },
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;adr&quot;: &quot;adr2&quot;,
          &quot;serviceCounty&quot;: &quot;West&quot;,
          &quot;area&quot;: &quot;city&quot;
        },
        {
          &quot;id&quot;: &quot;test&quot;,
          &quot;cusId&quot;: &quot;test&quot;,
          &quot;adr&quot;: &quot;test&quot;,
          &quot;serviceCounty&quot;: &quot;West&quot;,
          &quot;area&quot;: &quot;test&quot;
        }
    ],
    &quot;serviceAreas&quot;: [
         &quot;Adr 1, Center, village&quot;, &quot;adr2, West, city&quot;
  ]
  }
  </code></pre>
  <p>Any help is appreciated!</p>
  <p>Here is what I have tried, but it didn't work out:</p>
  <pre><code>'serviceAreas': {
      '$reduce': {
          'input': '$serviceImpactHistory.event.services',
          'initialValue': [],
          'in': {
              '$setUnion': [
                  '$$value', {'$concat': ['$$this.serviceCounty', '$$this.adr', '$$this.area']}
              ]
          }
      }
  },
  </code></pre>