title: How to conditionally project fields during aggregate in mongodb
body: <p>I have a user document like:</p>
  
  <pre><code>{
      _id: "s0m3Id",
      _skills: ["skill1", "skill2"],
  }
  </code></pre>
  
  <p>Now I want to unwind this document by the <code>_skills</code> field and add a score for each skill. So my aggregate looks like:</p>
  
  <pre><code>{ 
    '$unwind': {'path': '$_skills', 'preserveNullAndEmptyArrays': true},
  },
  {
    '$project': {
      '_skills':
        'label': '$_skills',
        'skill_score': 1
      },
    }
  },
  </code></pre>
  
  <p>Sometimes the <code>_skills</code> field can be empty, however in this case I still want the user document to flow through the aggregation - hence the <code>preserveNullAndEmptyArrays</code> parameter. However, the problem I'm having is that it will project a <code>skill_score</code> (though with no <code>label</code>) onto documents which had empty <code>_skills</code> array fields. Thus, when I go to <code>$group</code> the documents later on, those documents now have a non-empty <code>_skills</code> array, containing a single object, namely <code>{skill_score: 1}</code>. This is not what I want - I want documents which had empty (or non-existent) <code>_skills</code> fields to not have any skill_score projected onto them.</p>
  
  <p>So how can I <strong>conditionally project a field based on the existence of another field</strong>? Using <code>$exists</code> does not help, because that is intended for <em>querying</em>, not for boolean expressions.</p>