Trouble trying to append to multi-dimensional subdocument

199 views
Skip to first unread message

jp

unread,
Apr 22, 2011, 2:55:40 AM4/22/11
to mongodb-user
I cannot seem to get a 2D subdocument working using PHP driver. I can
$set the initial array, but when I try to $addToSet, $push or $pushAll
more elements to that array it does nothing. If I do a simple 1D array
and allow Mongo to assign the index I am successful.

Works:

$dataList =
array(
array( "_id"=>1, "value"=>"data1" ),
array( "_id"=>2, "value"=>"data2" ),
array( "_id"=>3, "value"=>"data3" ),
array( "_id"=>4, "value"=>"data4" ) );

$command = array( '$set' => array( "data" => $dataList ) );

$collection->update( array( "_id"=> 1 ), $command );

$newList =
array(
array( "_id"=>5, "value"=>"data5" ),
array( "_id"=>6, "value"=>"data6" ) );

$command =
array( '$pushAll' =>
array( "data" => $newList ) );

$collection->update( array( "_id"=> 1 ), $command );


Does not work:

$dataList = array();

$dataList[ 1 ][ 0 ] = array( "_id"=>1, "value"=>"data1" );
$dataList[ 1 ][ 1 ] = array( "_id"=>2, "value"=>"data2" );
$dataList[ 2 ][ 0 ] = array( "_id"=>3, "value"=>"data3" );
$dataList[ 2 ][ 1 ] = array( "_id"=>4, "value"=>"data4" );

$command = array( '$set' => array( "data" => $dataList ) );

$collection->update( array( "_id"=> 1 ), $command );

$newList = array();

$dataList[ 3 ][ 0 ] = array( "_id"=>5, "value"=>"data5" );
$dataList[ 3 ][ 1 ] = array( "_id"=>6, "value"=>"data6" );
$dataList[ 4 ][ 0 ] = array( "_id"=>7, "value"=>"data7" );
$dataList[ 4 ][ 1 ] = array( "_id"=>8, "value"=>"data8" );

$command =
array( '$pushAll' =>
array( "data" => $newList ) );

$collection->update( array( "_id"=> 1 ), $command );


Any Ideas? Any help is greatly appreciated.

Regards

Eliot Horowitz

unread,
Apr 22, 2011, 3:27:19 AM4/22/11
to mongod...@googlegroups.com
$newList never gets modified.
Was that a typo or am I missing something?

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Message has been deleted

jp

unread,
Apr 23, 2011, 1:44:37 AM4/23/11
to mongodb-user
Typo...Should have been:

$newList = array();
$newList[ 3 ][ 0 ] = array( "_id"=>5, "value"=>"data5" );
$newList[ 3 ][ 1 ] = array( "_id"=>6, "value"=>"data6" );
$newList[ 4 ][ 0 ] = array( "_id"=>7, "value"=>"data7" );
$newList[ 4 ][ 1 ] = array( "_id"=>8, "value"=>"data8" );

The first 4 values are added as a 2D array but the subsequent 4 do not
get added using $addToSet, $push or $pushAll.

Regards
Message has been deleted

jp

unread,
Apr 27, 2011, 12:55:44 AM4/27/11
to mongodb-user
In light of the fact that I cannot filter the subdocuments upon
querying, I cannot see a way around the need to store the subdocument
array based on a key that I can use to quickly locate the one subdoc I
need from all those returned.

I still have the problem that when I try to create my subdocument
array based on this key (rather than sequential int from 0...N), I am
unable to then push other subdocuments into that array.

Have others done this? It is a problem with the PHP driver? Is this
normal Mongo behavior?

Cheers
Reply all
Reply to author
Forward
0 new messages