Sokil\Mongo\Collection::update PHP Method

update() public method

Update multiple documents
public update ( Expression | array | callable $expression, Operator | array | callable $updateData, array $options = [] ) : Collection
$expression Expression | array | callable expression to define which documents will change.
$updateData Operator | array | callable new data or operators to update
$options array update options, see http://php.net/manual/ru/mongocollection.update.php
return Collection
    public function update($expression, $updateData, array $options = array())
    {
        // execute update operator
        $result = $this->getMongoCollection()->update(Expression::convertToArray($expression), Operator::convertToArray($updateData), $options);
        // if write concern acknowledged
        if (is_array($result)) {
            if ($result['ok'] != 1) {
                throw new Exception(sprintf('Update error: %s: %s', $result['err'], $result['errmsg']));
            }
            return $this;
        }
        // if write concern unacknowledged
        if (!$result) {
            throw new Exception('Update error');
        }
        return $this;
    }

Usage Example

Example #1
0
 public function testUpdate_UpdateData()
 {
     $this->collection->disableDocumentPool();
     // create documents
     $this->collection->createDocument(array('p' => 1))->save();
     $this->collection->update(array(), array('k' => 'v'));
     // test
     $data = $this->collection->find()->findOne()->refresh()->toArray();
     unset($data['_id']);
     $this->assertEquals(array('k' => 'v'), $data);
 }