title: How to flatten array into a new field in MongoDB
body: <p>I have a document with an array field named <code>enabled_domain</code> with 1, 2 or more elements. I need to flatten this array into a new field so it will be a string field with the concatenation of all the array field elements separated by comma, for example. </p>
  
  <p>What I have done so far is:</p>
  
  <pre><code>db.myCollection.aggregate(
     [
        { 
            $project: { 
                enabled_domain_2: { 
                    $reduce: {
                        input: "enabled_domain",
                        initialValue: "",
                        in: { 
                            $concat: [ '$$value', '$$this' ] 
                            }
                        }
                    }
                }
            }
     ]
  )
  </code></pre>
  
  <p>... but it does not work.</p>
  
  <p>A sample of <code>myCollection</code> is:</p>
  
  <pre><code>{
      "_id" : ObjectId("56c1fd43e4b0a6078c98108f"),
      "enabled_domain" : [ 
          "A", 
          "B"
      ]
  }
  
  /* 2 */
  {
      "_id" : ObjectId("5436044fb700a771a18eeac0"),
      "enabled_domain" : [ 
          "A"
      ]
  }
  </code></pre>
  
  <p>How can I make this operation? Thanks in advance.</p>