title: Conditionally Filter Documents in MongodDb
body: <p>I have to do this in aggregation pipleline.
  I have two fields that have to be used to decide if document can be included or not. </p>
  
  <p>First is  agentType- may have value 1 or 2.
  Second is agentImage - key may exist or not.</p>
  
  <p>What I am looking for is something like this.</p>
  
  <p>if (agentType === 1 &amp;&amp; agentImage not Exists) -> Filter out,
  If (agentType === 2) -> Keep document</p>
  
  <p>For agentType === 2 i want all documents irrespective of agentImage being there or not. </p>
  
  <p>Sample documents- </p>
  
  <pre><code>{agentType: 1, agentImage: "abc"}
  {agentType: 2}
  {agentType: 1, location: 'temp'}
  {agentType: 2, agentImage: "xyz"}
  </code></pre>
  
  <p>In this scenario the result should be-</p>
  
  <pre><code>{agentType: 1, agentImage: "abc"}
  {agentType: 2}
  {agentType: 2, agentImage: "xyz"}
  
  db.getCollection('agents').aggregate([{"$match":{agentImages: {'$exists':{'$or':[{$and:[{$eq:{'agentType': 1}},{$ne: {'agentImages': null}}]},{$eq: {'agentType': 2}}]}}}}])
  </code></pre>
  
  <p>This does not work, it filters out all documents with agentType 2 and with agentImage not there.</p>