在doctrine mongodb中对多个文档进行原子更新

时间:2013-01-17 17:49:00

标签: symfony doctrine-mongodb

我使用symfony2和doctrine-mongodb-odm开展项目。 我想用querybuilder对几个文件执行原子更新,但我遗漏了一些东西:

$this->createQueryBuilder('MyBundle:MyDoc')
->update()
->field('isOpen')->set(false)
->field('isOpen')->equals(true)
->getQuery()
->execute();

它可以工作,但它只更新一个文档。我想我应该添加一个像

这样的选项
array('multi' => true)

某处,但我在文档中没有找到任何相关内容。

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:2)

我通过查看类定义找到了答案。有一个名为multiple的查询构建器的方法来设置此选项。

$this->createQueryBuilder('MyBundle:MyDoc')
->update()
->multiple(true)
->field('isOpen')->set(false)
->field('isOpen')->equals(true)
->getQuery()
->execute();

答案 1 :(得分:1)

现在使用 multiple()已弃用。您只需使用 updateMany()即可。

/**
 * Set the "multiple" option for an update query.
 *
 * @param boolean $bool
 * @return $this
 *
 * @deprecated Deprecated in version 1.4 - use updateOne or updateMany instead
 */
public function multiple($bool = true)
{
    $this->query['multiple'] = (boolean) $bool;
    return $this;
}

/**
 * Change the query type to update multiple documents
 *
 * @return $this
 */
public function updateMany()
{
    $this->query['type'] = Query::TYPE_UPDATE;
    $this->query['multiple'] = true;
    return $this;
}