title: Alternative to $addToSet to process elements in an array (instead of the array as a whole)
body: <p>I have a  collection in MongoDB with documents which follow this pattern:</p>
  
  <pre><code>{
          "_id" : {
                  "id" : "ID1",
                  "type" : "TYPE1"
          },
          "attrs" : [
                  {
                          "name" : "ATTR1",
                          "value" : "foo"
                  },
                  {
                          "name" : "ATTR2",
                          "type" : "bar"
                  },
                  ...
                  {
                          "name" : "ATTRn",
                          "value" : "blabla"
                  }
          ]
  }
  </code></pre>
  
  <p>Each document in the collection represents an entity (with an unique ID and type) an a set of attributes. Each document can have a varible number of attributes, even belonging to the same type (i.e. two documents with the same <code>_id.type</code> could have different sets of attributes).</p>
  
  <p>I would like to get the names of the attributes associated to a given type (actually, the union of the attribute sets). I'm trying with the following:</p>
  
  <pre><code>db.runCommand({aggregate: "col", pipeline: [ {$group: {_id: "$_id.type", attr: {$addToSet: "$attrs.name"}} }]})
  </code></pre>
  
  <p>Which results in:</p>
  
  <pre><code>{
          "result" : [
                  {
                          "_id" : "TYPE1",
                          "attr" : [
                                  [
                                          "ATTR1",
                                          "ATTR2",
                                          "ATTR3"
                                  ],
                                  [
                                          "ATTR4",
                                          "ATTR5"
                                  ]
                          ]
                  },
                  ...                    
          ],
          "ok" : 1
  }
  </code></pre>
  
  <p>The problem is that <code>$addToSet</code> doesn't process element by element when adding the elements of an array. Instead of that, it considers the whole array as a single element an at it. Thus, at the end what I get is an "array of arrays" while what I would like to have is something like this:</p>
  
  <pre><code>{
          "result" : [
                  {
                          "_id" : "TYPE1",
                          "attr" : [
  
                                      "ATTR1",
                                      "ATTR2",
                                      "ATTR3",                                 
                                      "ATTR4",
                                      "ATTR5"
                          ]
                  },
                  ...                    
          ],
          "ok" : 1
  }
  </code></pre>
  
  <p>How the above query should be reformulated in order to get this result?</p>