【问题标题】:MongoDB increment multiple document array values in PHPMongoDB在PHP中增加多个文档数组值
【发布时间】:2012-10-28 15:58:20
【问题描述】:

我想递增集合中包含给定嵌套数组值的所有文档。我的每个对象都包含一个带有 key:number 值的“order”数组。

{
    _id: ...,
    order : array(
        foo: 34
    )
}

但是,我无法使用 PHP MongoDB Native Driver 找出正确的 MongoDB 查询。

    // Update all existing items with an order greater than this number
    $number = 2;

    $result = $collection->update(
        array("order" => array('foo' => array('$gt' => $number))),
        array('$inc' => array('order' => array('foo' => 1))),
        array("safe" => true)
    );

【问题讨论】:

    标签: php mongodb


    【解决方案1】:

    这是您的 MongoDB 查询的 PHP 版本,我另外添加了多个选项。

    $collection->update(array('order.foo'=>array('$gt'=>2)), array('$inc' => array('order.foo'=>1)), array('multiple'=>true, 'safe'=>true));
    

    【讨论】:

      猜你喜欢
      • 2014-05-28
      • 2021-06-26
      • 1970-01-01
      • 2019-05-27
      • 2020-11-17
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多