title: MongoDB: Update field using value from another field
body: <p>Using Mongo shell, I am trying to add a field which has the same value as an existing field for all documents in a collection. Assuming we have two documents :</p>
  
  <pre><code>{
     'foo': 'str1'
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{
     'foo': 'str2'
  }
  </code></pre>
  
  <p>I would like to insert a new field <code>'foo_new'</code> which has the respective value of <code>'foo'</code> as its value, so that the documents become</p>
  
  <pre><code>{
     'foo': 'str1'
     'foo_new': 'str1'
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{
     'foo': 'str2'
     'foo_new': 'str2'
  }
  </code></pre>
  
  <p>The command I use to update the collection ('coll' say) in Mongo shell is</p>
  
  <pre><code>db.coll.update({}, {$set: {'foo_new': '$foo'}}, {multi: true})
  </code></pre>
  
  <p>The result of running this command are the two updated documents</p>
  
  <pre><code>{
     'foo': 'str1'
     'foo_new': '$foo'
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{
     'foo': 'str2'
     'foo_new': '$foo'
  }
  </code></pre>
  
  <p>i.e. <code>'$foo'</code> is being interpreted as a literal for some reason.</p>