0

My problem is that when I submit a form and record it to MongoDB.

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.

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.

Here is how a document of my collection looks:

{
   "_id": ObjectId("58a44a61e09075e805d63af1"),
   "en": "Cat",
   "de": "Katze",
   "es": "Gato",
}

During Submit, I can edit it this way:

{
   "_id": ObjectId("58a44a61e09075e805d63af1"),
   "en": "Cat",
   "de": "Katze",
   "fr": "Chat",
}

Or this way:

{
   "_id": ObjectId("58a44a61e09075e805d63af1"),
   "en": "Cat",
   "de": "Katze",
   "es": "Gato",
   "fr": "Chat",
}

But I'm unable to do this:

{
   "_id": ObjectId("58a44a61e09075e805d63af1"),
   "en": "Cat",
   "de": "Katze"
}

Here is my form:

<form role="form" action="" method="post">
   <table>
     <?php
       foreach ($glossary as $term) {
         foreach (array_slice($keys,1) as $key => $value) {
            echo "<tr id='language" . $key . "' class='languages'>";
               echo "<td>";
                  echo "<select class='form-control' name='lang" . $key . "'>";
                    echo "<option value='' selected></option>";
                    // Options not shown for simplicity.
                  echo "</select>";
               echo "</td>";
               echo "<td>";
                  echo "<input class='form-control' value='$term[$value]' name='langInput" . $key . "'>";
               echo "</td>";
            echo "</tr>";
        }
      }
    }
    ?>
   </table>

 <input type="submit" name="submit" class="btn btn-primary" value="Save Changes">

On submit I do:

   $termArray = array();

   // Get submitted form vars
   for ($i = 0; $i <= $loop; $i++ ) {
       $langCode = cleanFormFld($_POST["lang".$i]);
       $termArray[$langCode] = cleanFormFld($_POST["langInput".$i]);
   }

   try {          
       $connection = new Mongo("mongodb://$dbhost");
       $db = $connection->$dbname;

       $db->terms->update(array('_id' => new MongoId($id)), array('$set' => $termArray));
   }
   catch(PDOException $e) {
       echo "Error: " . $e->getMessage();
   }

Every langX and langInputX are created dinamicaly in jQuery.

2
  • Maybe I'm confused, but you need $pull instead of $set to remove array elements Commented Feb 17, 2017 at 17:27
  • Thanks hd, but I just used $pull but nothing happened. Commented Feb 17, 2017 at 17:36

2 Answers 2

1
$db->terms->update(array('_id' => new MongoId($id)), array('$set' => $termArray));

Using $set modifier doesn't allow you to unset fields, it aims to update fields you specify. If you want to replace whole document (effectively having only the data you're passing to the function) you should write

$db->terms->update(array('_id' => new MongoId($id)), $termArray);
Sign up to request clarification or add additional context in comments.

Comments

0

I manage to do it work by removing the document, and adding it again.

$db->terms->remove( array( '_id' => new MongoId($id)));

$db->terms->insert($termArray);
$id = $termArray['_id'];

Although it works, I don't like this solution at all. Any ideas?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.