title: PHP MongoDB unset fields on update
body: <p>My problem is that when I submit a form and record it to MongoDB.</p>
  
  <p>I am able to add new fields to a document using update, I can change their values and keys, but I'm not able to remove / unset them.</p>
  
  <p>I'm pretty new to Mongo. I normally work with the stack: MySQL+PHP+Bootstrap, but for production reasons we need to add Mongo to the equation.</p>
  
  <p>Here is how a document of my collection looks:</p>
  
  <pre><code>{
     "_id": ObjectId("58a44a61e09075e805d63af1"),
     "en": "Cat",
     "de": "Katze",
     "es": "Gato",
  }
  </code></pre>
  
  <p>During Submit, I can edit it this way:</p>
  
  <pre><code>{
     "_id": ObjectId("58a44a61e09075e805d63af1"),
     "en": "Cat",
     "de": "Katze",
     "fr": "Chat",
  }
  </code></pre>
  
  <p>Or this way:</p>
  
  <pre><code>{
     "_id": ObjectId("58a44a61e09075e805d63af1"),
     "en": "Cat",
     "de": "Katze",
     "es": "Gato",
     "fr": "Chat",
  }
  </code></pre>
  
  <p>But I'm unable to do this:</p>
  
  <pre><code>{
     "_id": ObjectId("58a44a61e09075e805d63af1"),
     "en": "Cat",
     "de": "Katze"
  }
  </code></pre>
  
  <p>Here is my form:</p>
  
  <pre><code>&lt;form role="form" action="" method="post"&gt;
     &lt;table&gt;
       &lt;?php
         foreach ($glossary as $term) {
           foreach (array_slice($keys,1) as $key =&gt; $value) {
              echo "&lt;tr id='language" . $key . "' class='languages'&gt;";
                 echo "&lt;td&gt;";
                    echo "&lt;select class='form-control' name='lang" . $key . "'&gt;";
                      echo "&lt;option value='' selected&gt;&lt;/option&gt;";
                      // Options not shown for simplicity.
                    echo "&lt;/select&gt;";
                 echo "&lt;/td&gt;";
                 echo "&lt;td&gt;";
                    echo "&lt;input class='form-control' value='$term[$value]' name='langInput" . $key . "'&gt;";
                 echo "&lt;/td&gt;";
              echo "&lt;/tr&gt;";
          }
        }
      }
      ?&gt;
     &lt;/table&gt;
  
   &lt;input type="submit" name="submit" class="btn btn-primary" value="Save Changes"&gt;
  </code></pre>
  
  <p></p>
  
  <p>On submit I do:</p>
  
  <pre><code>   $termArray = array();
  
     // Get submitted form vars
     for ($i = 0; $i &lt;= $loop; $i++ ) {
         $langCode = cleanFormFld($_POST["lang".$i]);
         $termArray[$langCode] = cleanFormFld($_POST["langInput".$i]);
     }
  
     try {          
         $connection = new Mongo("mongodb://$dbhost");
         $db = $connection-&gt;$dbname;
  
         $db-&gt;terms-&gt;update(array('_id' =&gt; new MongoId($id)), array('$set' =&gt; $termArray));
     }
     catch(PDOException $e) {
         echo "Error: " . $e-&gt;getMessage();
     }
  </code></pre>
  
  <p>Every langX and langInputX are created dinamicaly in jQuery.</p>