title: Conditionally Project Matching Array Item
body: <p>I have a collection named <code>questions</code> with documents like this:</p>
  
  <pre><code>{
      "formats": [
          {
              "language_id": 1,
              "text": "question text1"
          },
          {
              "language_id": 2,
              "text": "question text 2"
          }
      ],
      "qid": "HQSRFA3T"
  }
  </code></pre>
  
  <p>I want to write a query such that if a specific <code>language_id</code> is not present, then <code>language_id</code> with 1 should be returned by default.</p>
  
  <p>I've tried two queries so far:</p>
  
  <pre><code>db.questions.aggregate([
    { 
      $match: {
        'qid': 'HQSRFA3T'
      }
    },
    {
      $project: {
        formats: {
          $ifNull: [
            { $filter: { input: '$formats', as: 'format', cond: {$eq: ['$$format.language_id', 3]}} },
            { $filter: { input: '$formats', as: 'format', cond: {$eq: ['$$format.language_id', 1]}} }
          ]
        },
        _id: 0
      }
    }
  ])
  </code></pre>
  
  <p>This query results in something like this:</p>
  
  <p><code>{ "formats" : [ ] }</code></p>
  
  <p>Then there's another query which is something like this:</p>
  
  <pre><code>db.questions.aggregate([ { $match: {'qid': 'HQSRFA3T'}}, { $project: {
    formats: {
      $filter: {
        input: '$formats',
        as: 'format',
        cond: {
          $or: [
            { $eq: ['$$format.language_id', 1] },
            { $eq: ['$$format.language_id', 3] }
          ]
        }
      }
    },
    _id: 0
  }}])
  </code></pre>
  
  <p>This query returns two elements in case both <code>language_id</code>'s are present in the array.</p>