title: Adding an array field to a collection based on values of another array field in mongodb
body: <p>I have a collection which has the following structure as denoted by one document</p>
  
  <pre><code>{
  _id : 1,
  array1 : [{fld1 : 'doc1e1fld1', fld2: 'doc1e1fld2'},{fld1:'doc1e2fld1',fld2: 'doc1e2fld2'}]
  }
  </code></pre>
  
  <p>I would like to add another field to ALL elements in the collection and set the value such that the modified doc above looks like : </p>
  
  <pre><code>{
      _id : 1,
      array1 : [{fld1 : 'doc1e1fld1', fld2: 'doc1e1fld2'},{fld1:'doc1e2fld1',fld2: 'doc1e2fld2'}],
      array2 : ['doc1e1fld1','doc1e2fld1']
      }
  </code></pre>
  
  <p>Basically adding a new array element to all documents in the collection and setting its contents to an array which is the fld1 value of all elements in the array1 of the document.</p>
  
  <p>I had a look at <a href="https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field">Update MongoDB field using value of another field</a> but somehow i dont understand how i can extract ony certain elements .</p>