在CakePHP MongoDB中使用unset

时间:2014-03-24 12:38:34

标签: mongodb cakephp

我正在使用 Ichikawa CakePHP MongoDB插件。我在使用unset时遇到问题。我在shell中尝试过命令:

 db.patents.update({}, {$unset : {"lv.2" : 1 }},{'multi':true})
 db.patents.update({},{$pull:{pid:"2"}},{'multi':true})

这些工作正常。

但是当我将它们转换为CakePHP命令时,如下所示:

 $this->Detail->updateAll(array('$unset'=>array('lv.2'=>1,array('multi'=>true))));

然后它不起作用并给出错误:

 MongoCollection::update(): expects parameter 1 to be an array or object, boolean given

任何人都可以帮我解决问题。

感谢。

1 个答案:

答案 0 :(得分:0)

没有条件

错误消息表示正在生成的查询等效于:

db.details.update(true

这可以通过查看query log来确认(如果您使用的是debug kit,则很简单)。

这是怎么回事?

缺少模型updateAll的第二个参数,这意味着它将具有默认值:

public function updateAll($fields, $conditions = true) {
                                                 ^
    return $this->getDataSource()->update($this, $fields, null, $conditions);
}

因此,在mongodb datasource class中,传递的条件为true

public function updateAll(&$Model, $fields = null,  $conditions = null) {
                                                    ^

因此,结果更新语句将true作为第一个参数,而不是数组。

正确的语法

此类查询的正确语法是:

$this->Detail->updateAll(
    array('$unset'=>array('lv.2'=>1))
    array() # <- do not omit this
);

请注意,没有必要将'multi'=>true指定为the datasource does that for you,尤其不要在fields参数中指定。