【问题标题】:mongodb $pull not working phpmongodb $pull 不工作 php
【发布时间】:2012-06-09 05:53:27
【问题描述】:

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

    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)

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

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

当我使用这个 $unset 而不是 $pull 时,满的 [folder] 将会消失。

【问题讨论】:

  • 只是猜测,但您的 $id 是字符串还是正确的 MongoID?即$real_id = new MongoId("$id"); ?? php.net/manual/en/mongoid.construct.php
  • 是MongiID,没问题。我也不能用这个拉根: $usersCollection->update(array("username" => "foouser"), array('$pull'=> array('folder'=>'root')));

标签: php mongodb pull unset


【解决方案1】:

您需要指定数组元素的 full 值。您还必须使用 'folder.root.feeds',因为这是您要从中提取的字段。您不希望将folder 中的数组元素拉出以免受到影响。

在 shell 上,你会运行:

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',
            )
        )
    )
);

【讨论】:

  • 为我工作:derickrethans.nl/files/dump/so-10893490.php.txt 我希望你有最新版本的 MongoDB?
  • 只需复制您的代码并工作。我认为问题是我的插入。另一个问题,如果我不知道“root”,我该如何使用 $?文件夹.$.feeds?
猜你喜欢
  • 2015-05-30
  • 1970-01-01
  • 2011-01-26
  • 2012-07-21
  • 2018-04-16
  • 2019-07-15
  • 2016-07-25
  • 2021-07-27
  • 2014-11-09
相关资源
最近更新 更多