title: Mongodb update array with another field
body: <p>I have a DB with a set of fields f1,f2,f3, where f3 is an array. I would like to do an update operation such as this</p>
  
  <blockquote>
    <p>db.collection.update ({f1:1},{$push:{f3:{$each:[<strong>f2's value</strong>],$slice:-2}}})</p>
  </blockquote>
  
  <p>I searched on the internet and was not abole to find anything. I am not sure if this is even possible. Any help would be much appreciated.</p>
  
  <p>EXAMPLE:</p>
  
  <p>This is my set of documents:</p>
  
  <pre><code>doc1 = { name: "User1", score: "Good",  History of scores: ["Good", "Bad", "Average", "Bad"] }
  doc2 = { name: "User2", score: "Bad",  History of scores: ["Good", "Average", "Average", "Bad"] }
  doc3 = { name: "User3", score: "Good",  History of scores: ["Good", "Good", "Average", "Good"] }
  </code></pre>
  
  <p>Now suppose we have to insert the corresponding data:</p>
  
  <pre><code>{name : "User1", score: "Good"}
  </code></pre>
  
  <p>I would like the document to update user1's history of scores so that doc1 becomes as follows:</p>
  
  <pre><code>doc1 = { name: "User1", score: "Good", History of scores: ["Bad", "Average", "Bad", "Good"] }
  </code></pre>
  
  <p>another of the same update should change doc1 to:</p>
  
  <pre><code>doc1 = { name: "User1", score: "Good", History of scores: ["Average", "Bad", "Good", "Good"] }
  </code></pre>
  
  <p>I hope now my question has become clearer. Thanks.</p>