Hey,
$addToSet works with *arrays* in JSON/BSON, but you're tring to set a
nested field in a document's field (contact). What you need to do, is
just set it with this:
$collection->update(
array("namespace" => "test"),
array(
'$set' => array("contact.web" => "
te...@test.com")
),
);
You don't need the upsert either, and also please note that the return
value from update, is not the udpated document, so I am not sure how
your example did that.
cheers,
Derick
On Fri, 24 May 2013,
demi...@ilyasdemirtas.com wrote:
> Hello everybody,
>
>
> How Can I push to sub-document in update ?,
>
> I wrote some code, but not the way I want.
>
> My code :
>
> INSERT CODE :
>
> $collection->insert(
> array(
> "namespace" => "test",
> "age" => 20,
> "contact" => array("hotmail" =>
> "
te...@hotmail.com.tr", "gmail" => "
te...@gmail.com")
> )
> );
>
>
> UPDATE CODE :
>
> $update_result = $update = $collection->update(
> array("namespace" => "test"),
> array(
> '$addToSet' => array("contact.web"
> => "
te...@test.com")
> ),
>
> array("upsert" => true)
> );
>
> var_dump($update_result);
>
> array(4) {
> ["_id"]=>
> object(MongoId)#6 (1) {
> ["$id"]=>
> string(24) "519f7cd1b824e6ab0c340933"
> }
> ["namespace"]=>
> string(4) "test"
> ["contact"]=>
> array(3) {
> ["gmail"]=>
> string(14) "
te...@gmail.com"
> ["hotmail"]=>
> string(19) "
te...@hotmail.com.tr"
> *["web"]=>
> array(1) { // Record has been added as an array, I want add record to old array
> [0]=>
> string(13) "
te...@test.com"
> }*
> }
> ["age"]=>
> int(20)
> }
>
> I want this result;
>
>
>
> array(4) {
> ["_id"]=>
> object(MongoId)#6 (1) {
> ["$id"]=>
> string(24) "519f7cd1b824e6ab0c340933"
> }
> ["namespace"]=>
> string(4) "test"
> ["contact"]=>
> array(3) {
> ["gmail"]=>
> string(14) "
te...@gmail.com"
> ["hotmail"]=>
> string(19) "
te...@hotmail.com.tr"
> *["web"]=>
> string(13) "
te...@test.com"*
> }
> ["age"]=>
> int(20)
> }
>
>
> I'm sorry bad english language, you are understand my problem from my says
> :) ?
>
>
--
{
website: [ "
http://mongodb.org", "
http://derickrethans.nl" ],
twitter: [ "@derickr", "@mongodb" ]
}