title: Insert if not exist else push into array(as well as update old values) in mongodb php?
body: <p>I ve a mongodb collection in the form of</p>
  
  <pre><code>{ "_id" : ObjectId("50072b17b4a6de3b1100000f")
   "employment_details" : [
   {
           "_id" : ObjectId("50072b17b4a6de3b11000015"),
           "title" : "Information Technology Compliance &amp; Security",
           "rank" : null,
           "department" : null,
           "current" : true,
           "company_id" : ObjectId("5007269fb4a6de941001d19d")
   }
   ]
  }
  </code></pre>
  
  <p>What I need is if the "<strong>employment_details</strong>" array is empty or does not exist i need to create and insert new values into the employment_details, else I need to <strong>push</strong> details like this,</p>
  
  <pre><code> { "_id" : ObjectId("50072b17b4a6de3b1100000f")
   "employment_details" : [
   {
           "_id" : ObjectId("50072b17b4a6de3b11000015"),
           "title" : "Information Technology Compliance &amp; Security",
           "rank" : null,
           "department" : null,
           "current" : false,
           "company_id" : ObjectId("5007269fb4a6de941001d19d")
   }
    ......
   {
           "_id" : ObjectId("50072b17b4a6de3b11000018"),
           "title" : "security engineer",
           "rank" : null,
           "department" : null,
           "current" : false,
           "company_id" : ObjectId("5007269fb4a6de941001dasd")
   }
   {
           "_id" : ObjectId("50072b17b4a6de3b11000016"),
           "title" : "software Engineer",
           "rank" : null,
           "department" : null,
           "current" : true,
           "company_id" : ObjectId("5007269fb4a6de94100189e")
   }
  ]
  }
  </code></pre>
  
  <p>But the tricky part is, since i am pushing employment details of a particular employee, the "<strong>current</strong>" field inside the "<strong>employment_details</strong>" must be set <strong>true</strong> only for newly inserted contact, the all other "<strong>current</strong>" field in old <strong>employment_details</strong> must be set <strong>false</strong>, like i mentioned in the above example.</p>
  
  <p>Actually, when a new <strong>employement_detail</strong> is inserted its <strong>current</strong> field must be set <strong>true</strong>, and all other <strong>current</strong> field in previous employment_details must be set to <strong>false</strong>.</p>
  
  <p>I know general syntax for pushing values, but how to update old values and push new values?</p>
  
  <p>Hope I am clear with my question.</p>
  
  <p>Update code(What I've tried):</p>
  
  <pre><code>$my_collection-&gt;update(
    array("_id"=&gt;$id), 
    array('$push' =&gt; 
     array(
       "employment_details" =&gt; array("title" =&gt; $title, "rank" =&gt; $rank, "department" =&gt; $dept, "current" =&gt; "true", "company_id" =&gt; $c_id)
    ))
  );
  </code></pre>
  
  <p>But I don't know how to update previous values.</p>