title: How to get a list of unique elements of each type of a particular field- MongoDB
body: <p>Suppose we have the following collection</p>
  
  <pre><code>[
      {
          'item' : 'A',
          'colors' : [
                 'a1', 'a2', 'a3'
          ]
      },
      {
          'item' : 'B',
          'colors' : [
                 'b1', 'b2', 'b3'
          ]
      },
      {
          'item' : 'A',
          'colors' : [
                 'c1', 'c2', 'c3'
          ]
      },
      {
          'item' : 'A',
          'colors' : [
                 'd1', 'd2', 'd3'
          ]
      }
  ]
  </code></pre>
  
  <p>I want to get the distinct values of each type of field. For example, in the above collection, I want to get the output as follows : </p>
  
  <pre><code>[
      {
          'item' : 'A',
          'colors' : [
                 'a1', 'a2', 'a3','c1', 'c2', 'c3','d1', 'd2', 'd3'
          ]
      },
      {
          'item' : 'B',
          'colors' : [
                 'b1', 'b2', 'b3'
          ]
      }
  ]
  </code></pre>
  
  <p>I tried the following query : <code>db.collection.aggregate([{$group:{_id:'$item',colors:{$addToSet : '$colors'}}}])</code> . But, this appends each individual list instead of generating a single list of only value. Something like this.</p>
  
  <pre><code>[
      {
          'item' : 'A',
          'colors' : [
                 ['a1', 'a2', 'a3'],['c1', 'c2', 'c3'],['d1', 'd2', 'd3']
          ]
      },
      {
          'item' : 'B',
          'colors' : [
                 'b1', 'b2', 'b3'
          ]
      }
  ]
  </code></pre>
  
  <p>What should I change in my query? Thanks a lot in advance for your help.</p>