title: MongoDB - Aggregation - To get unique items in array
body: <p>Here's my MongoDB collection:</p>
  
  <pre><code>{
      "_id" : ObjectId("515d8f53175b8ecb053425c2"),
      "category" : "Batteries",
      "products" : [
          {
              "brand" : "Duracell",
              "item" : [
                  "AA",
                  "AAA"
              ]
          },
          {
              "brand" : "Everyday",
              "item" : [
                  "9V",
                  "AA",
                  "12V"
              ]
          }
      ]
  }
  </code></pre>
  
  <p>The output that I need is </p>
  
  <p>1) Unique list of all items</p>
  
  <pre><code>{["AA", "AAA", "9V", "12V"]}
  </code></pre>
  
  <p>and 
  2. unique list of items per product</p>
  
  <pre><code>{
      "category" : "Batteries",
      "item": ["AA", "AAA", "9V", "12V"]
  }
  </code></pre>
  
  <p>I'm very new to MongoDB, and I tried different aggregations functions and nothing seems to work. Please help. </p>