title: How do I use a variable as a field name in a Mongo query in Meteor?
body: <p>How would I go about using a variable as a field name in a Mongo query in a Meteor application.</p>
  
  <p>Here is an example...</p>
  
  <p>This runs a find on my request controllers collection after capitalizing the collection name for the parent id of a child.  The child is the <b>users</b> field.</p>
  
  <pre><code>window[Meteor.request.controller.capitalise()]["find"]({ _id: Session.get('parent_id'), users: params.child }).count()
  </code></pre>
  
  <p>As you can see my controller is a variable name for the collection item which allows me to have a single line of code for finding children of controller/collections but I need to be able to set the child field name to a variable.  In the above example that would be <b>users</b> but I want it to be a variable name.  </p>
  
  <p>I have tried this but it does not work.</p>
  
  <pre><code>window[Meteor.request.controller.capitalise()]["find"]({ _id: Session.get('parent_id'), [Session.get('child_collection_name').decapitalise()]: params.child }).count()
  </code></pre>
  
  <p>where</p>
  
  <pre><code>Session.get('child_collection_name').decapitalise()
  </code></pre>
  
  <p>returns <strong>users</strong></p>
  
  <p>Any ideas?  If I can figure out how to use a variable name in a mongo query in meteor it would reduce my code footprint significantly.</p>