title: MongoDB - count unique values of several arrays
body: <p>I'm trying to find a way to get the unique values of several arrays in the same document and across documents. It's best explained with an example:</p>
  
  <pre><code>[
      {
        _id: "x",
        products: {
          product_a: ["v1", "v2"],
          product_b: ["v3", "v2"]
        }
      },
      {
        _id: "y",
        products: {
          product_a: ["v1"],
          product_b: ["v3", "v4"]
        }
      }
  ]
  </code></pre>
  
  <p>What i'm trying to get is:</p>
  
  <ol>
  <li>The number of unique values for each document. There are 3 unique
  values for products in 'x' and 3 unique values in 'y'.</li>
  <li>The number of
  unique values overall. There are 4 unique values for all documents
  in the collection.</li>
  </ol>