title: mongodb using projection in $filter
body: <p>I have inserted a sample document </p>
  
  <pre><code>db.test.insert({
      x:1, 
      a:[
          {b:1,c:1,d:1},
          {b:2,c:2}
        ]
  })
  </code></pre>
  
  <p>I am facing 2 problems when I try to use $fitler aggregation as in my below query</p>
  
  <pre><code>db.test.aggregate(
  {$project:{ 
    a:{$filter:{
      input : '$a',
      as : 'item',
      cond : '$$item.d'
    }}
  }}
  )
  </code></pre>
  
  <h1>Element Existence</h1>
  
  <p>1] How do I test the existence of <strong>element a.d</strong>, I found a way of just using <strong>cond : '$$item.d'</strong>, but I think there should be a better way of doing it.</p>
  
  <h1>Selective Projection</h1>
  
  <p>2] How do I selectively project <strong>b and d nodes</strong>.
  I tried the below code and it works, but I think there is a pipeline in projection as well. Therefore I applied projection twice on the same node 1 for filtering array elements, 2 for array element nodes</p>
  
  <pre><code>db.test.aggregate(
  {$project:{ 
    a:{$filter:{
      input : '$a',
      as : 'item',
      cond : '$$item.d'
    }},
    a:{b:1, d:1}
  }}
  )
  </code></pre>
  
  <p>I seem to get the solution, but I think there may be a better way. Thanks for reply!</p>