title: How to use variables in MongoDB Map-reduce map function
body: <p>Given a document </p>
  
  <pre><code>{_id:110000, groupings:{A:'AV',B:'BV',C:'CV',D:'DV'},coin:{old:10,new:12}}
  </code></pre>
  
  <p>My specs call for the specification of attributes for mapping and aggregation at run time, as the groupings the user is interested in are not known up front, but specified by the user at runtime.</p>
  
  <p>For example, one user would specify [A,B] which will cause mapping emissions of</p>
  
  <pre><code>emit( {A:this.groupings.A,B:this.groupings.B},this.coin )
  </code></pre>
  
  <p>while another would want to specify [A,C] which will cause mapping emissions of</p>
  
  <pre><code>emit( {A:this.groupings.A,C:this.groupings.C},this.coin )
  </code></pre>
  
  <p>B/c the mapper and reducer functions execute server side, and don't have access to client variables, I haven't been able to come up with a way to use a variable map key in the mapper function. </p>
  
  <p>If I could reference a list of things to group by from the scope of the execution of the map function, this is all very straightforward. However, b/c the mapping function ends up getting these from a different scope, I don't know how to do this, or if it's even possible.</p>
  
  <p>Before I start trying to dynamically build java script to execute through the driver, does anyone have a better suggestion? Maybe a 'group' function will handle this scenario better?</p>