0

I have this object in my db.

    Array(
[_id] => MongoId Object
    (
        [$id] => 4fcdb3b8749d786c03000002
    )

[email] => foo@bar.com
[folder] => Array
    (
        [root] => Array
            (
                [feeds] => Array
                    (
                        [0] => Array
                            (
                                [feedID] => MongoId Object
                                    (
                                        [$id] => 4fcdaa9e749d786c03000001
                                    )

                                [title] => title.com
                            )

                    )

                [title] => root
            )

    )


[status] => 1
[username] => foouser)

I want pull just item [0] from [feeds]. I use this code but it not working:

$usersCollection->update(array("username" => "foouser"), array('$pull'=> array('folder'=>array('root'=>array('feeds'=>array('feedID'=>$id))))));

When I use this $unset instead of $pull, full of [folder] will be gone.

Arash
  • 3
  • 2
  • Just a guess, but is your `$id` a string, or a proper MongoID? i.e. `$real_id = new MongoId("$id");` ?? http://www.php.net/manual/en/mongoid.construct.php – Justin Jenkins Jun 05 '12 at 08:25
  • It's a MongiID,it's not problem. I can not pull root also with this: $usersCollection->update(array("username" => "foouser"), array('$pull'=> array('folder'=>'root'))); – Arash Jun 05 '12 at 08:31

1 Answers1

1

You need to specify the full value of the array element. You also have to use 'folder.root.feeds' as that's the field you want to pull from. You don't want to pull the array element inside folder from being affected.

On the shell, you would run:

db.so.update(
    { 'username' : 'foouser' },
    { $pull:
        { 'folder.root.feeds' :
            {
                "feedId" : ObjectId("4fcdaa9e749d786c03000001"),
                "title" : "title.com"
            }
        }
    }
);

And in PHP, that would translate to:

$c->update(
    array( 'username' => 'foouser' ),
    array( '$pull' =>
        array( 'folder.root.feeds' =>
            array(
                'feedId' => new MongoId('4fcdaa9e749d786c03000001'),
                'title' => 'title.com',
            )
        )
    )
);
Derick
  • 35,169
  • 5
  • 76
  • 99
  • Works for me: http://derickrethans.nl/files/dump/so-10893490.php.txt You do have a recent version of MongoDB I hope? – Derick Jun 05 '12 at 13:54
  • just copy your code and worked.I think problem was my insert.another question,how can I use the $ if I dont know 'root'? folder.$.feeds? – Arash Jun 05 '12 at 19:35