title: How to declare a variable from a field of the document inside a reduce or map in a aggregation?
body: <p>I have a collection like following:</p>
  
  <pre><code>{
    id: 1,
    names: {
      first: 'Ana',
      second: '',
      third: 'Smith',
      four: 'Smitchd'
    },
    used: [
      {
        position: 'first',
        id: 'A'
      },
      {
        position: 'second',
        id: 'B'
      },
    ] 
  }
  </code></pre>
  
  <p>I need count how many <code>used names</code> have the collections through the documents excluding thoses are empty.</p>
  
  <p>The position of <code>names</code> used are saved inside a array called <code>used</code>. </p>
  
  <p>In the example i need count the names which exists inside <code>used</code>, but in <code>names</code> is not empty</p>
  
  <p>I tried a aggregation using a reduce to walk through the arrays <code>used</code>, and i can count the items inside the arrays, but i cant use the field names inside the reduce.</p>
  
  <pre><code>db.collection.aggregate([
    { $match: id: 1 },
    { $project: { 
      totalUsed: $reduce: {
        input: '$used',
        initialValue: 0,
        in: { $add: ['$$value', { $cond: [{ $eq: ["$$name.$$this.position", '' ]}, 1, 0] }]}
      }
    }}
  ])
  </code></pre>
  
  <p>I nedd obtain the count of no empty names used... In the example i should get count: 1, because, even when in used existe first and secodn, the field names.second is empty.</p>