MongoDB$pull无法正常工作PHP


mongodb $pull not working php

我的数据库中有这个对象。

    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)

我只想从 [提要] 中提取项目 [0]。我使用此代码,但它不起作用:

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

当我使用此$unset而不是$pull时,充满[文件夹]将消失。

您需要指定数组元素的完整值。您还必须使用"folder.root.feeds",因为这是您要从中提取的字段。您不希望拉取folder内的数组元素不受影响。

在外壳上,您将运行:

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

在PHP中,这将转化为:

$c->update(
    array( 'username' => 'foouser' ),
    array( '$pull' =>
        array( 'folder.root.feeds' =>
            array(
                'feedId' => new MongoId('4fcdaa9e749d786c03000001'),
                'title' => 'title.com',
            )
        )
    )
);