Mongodb / PHP更新,删除集合中的记录并抛出异常。 为什么?

The contents of my mongodb collection are in the form of

{
"_id" : ObjectId("50072668b4a6de94100000bb"),
"addresses" : [
    {
            "_id" : ObjectId("50072668b4a6de94100000bf"),
            "address1" : "66 Wadsworth Park Dr",
            "address2" : null,
            "city" : "Draper",
            "country" : "United States",
            "country_code" : "US",
            "name" : "main",
            "primary" : true,
            "state" : "Utah",
            "zip" : "84020"
    }
 ]
}

I am trying to add another address details inside the collection, so that the content will become,

{
 "_id" : ObjectId("50072668b4a6de94100000bb"),
  "addresses" : [
  {
        "_id" : ObjectId("50072668b4a6de94100000bf"),
        "address1" : "66 Wadsworth Park Dr",
        "address2" : null,
        "city" : "Draper",
        "country" : "United States",
        "country_code" : "US",
        "name" : "main",
        "primary" : true,
        "state" : "Utah",
        "zip" : "84020"
   } 
   {
        "_id" : ObjectId("50072668b4a6de9410000023"),
        "address1" : "a",
        "address2" : "b",
        "city" : "c",
        "country" : "d",
        "country_code" : "d",
        "name" : "e",
        "primary" : "f",
        "state" : "g",
        "zip" : "h"
   } 
 ]
}

Here is the code I have written to update address field.

$add_address = array('$addToSet' => array("addresses" => array("_id" => new MongoId(), "address1" => $address1, "address2" => $address2, "city" => $city, "country" => $country, "country_code" => $country_code, "name" => $name, "primary" => $primary, "state" => $state, "zip" => $zip)));
$mycollection->update(array("_id" => new MongoId($id)), $add_address);

But this code is not updating the contents, Meanwhile it deletes the whole record. and the exception message I got is,

  Fatal error: Uncaught exception 'MongoCursorException' with message 'false'

The "addresses" I have is an embedded object. I am wondering whether we can use 'addToSet' on embedded object.

Where I am going wrong?

Have you try following way to update?

$add_address = array('$set' => array("addresses" => array("_id" => new MongoId(), "address1" => $address1, "address2" => $address2, "city" => $city, "country" => $country, "country_code" => $country_code, "name" => $name, "primary" => $primary, "state" => $state, "zip" => $zip)));
$mycollection->update(array("_id" => new MongoId($id)), $add_address);

$addToSet is used to append a field to an array (if it doesn't already exist).

What you should do is use the $set update modifier:

$add_address = array('addresses' => array("_id" => new MongoId(), "address1" => $address1, "address2" => $address2, "city" => $city, "country" => $country, "country_code" => $country_code, "name" => $name, "primary" => $primary, "state" => $state, "zip" => $zip));
$mycollection->update( array( array("_id" => new MongoId($id) ), array('$set' => $add_address) );

And $unset is used to delete a field.