title: MongoDB: returning dynamic field from the result
body: <p>Consider a collection,</p>
  <pre><code>MongoDB Enterprise rs0:PRIMARY&gt; db.col.find({});
  { &quot;_id&quot; : 1, &quot;height&quot; : 150, &quot;weight&quot; : 60 }
  </code></pre>
  <p>I want to write a function to dynamically return the field value based on input.</p>
  <pre><code>function getFieldValue(inputField){
      
      var resultDoc = db.col.findAndModify({
          query:{_id: 1 },
          update: {$inc:{[inputField]:1}},
          new:true
          });
      
      return resultDoc;
  }
  </code></pre>
  <p>This will return the whole document. How can I return only the field that was passed as input?</p>
  <pre><code>MongoDB Enterprise rs0:PRIMARY&gt; var x = getFieldValue(&quot;weight&quot;)
  MongoDB Enterprise rs0:PRIMARY&gt; x
  61
  </code></pre>
  <p>I tried <code>resultDoc.[inputField]</code>, but that causes syntax error.</p>