title: Unique field values for subset of documents
body: <p>Here is a MongoDB query and aggregation puzzle.  Fist some sample records:</p>
  
  <pre><code># Document 1
  {
    items: [
      {
        type: "X",
        id: 123
      },
      {
        type: "Y",
        id: 456
      }
    ]
  }
  
  # Document 2
  {
    items: [
      {
        type: "A",
        id: 789
      },
      {
        type: "B",
        id: 321
      }
    ]
  }
  
  # Document 3
  {
    items: [
      {
        type: "P",
        id: 987
      },
      {
        type: "X",
        id: 654
      }
    ]
  }
  
  # Document 4
  {
    items: [
      {
        type: "Q",
        id: 246
      },
      {
        type: "X",
        id: 654
      }
    ]
  }
  </code></pre>
  
  <p>My goal is to find all the distinct <code>id</code> values for documents which contain a <code>type: X</code> where the <code>id</code> is the <code>id</code> associated with the <code>type: X</code> element.</p>
  
  <p>For example, in the above, what I would like my result to be is:</p>
  
  <p><code>[ 123, 654 ]</code></p>
  
  <p>These are the unique values of the <code>id</code> field associated with the <code>type: X</code>.</p>