UpdateMany.php 3.26 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2015-2017 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17 18 19 20 21

namespace MongoDB\Operation;

use MongoDB\UpdateResult;
use MongoDB\Driver\Server;
22
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
23
use MongoDB\Exception\InvalidArgumentException;
24
use MongoDB\Exception\UnsupportedException;
25 26 27 28 29

/**
 * Operation for updating multiple documents with the update command.
 *
 * @api
30
 * @see \MongoDB\Collection::updateMany()
31 32 33 34 35 36 37 38 39 40 41
 * @see http://docs.mongodb.org/manual/reference/command/update/
 */
class UpdateMany implements Executable
{
    private $update;

    /**
     * Constructs an update command.
     *
     * Supported options:
     *
42 43 44 45 46
     *  * bypassDocumentValidation (boolean): If true, allows the write to
     *    circumvent document level validation.
     *
     *    For servers < 3.2, this option is ignored as document level validation
     *    is not available.
47
     *
48 49 50 51 52
     *  * collation (document): Collation specification.
     *
     *    This is not supported for server versions < 3.4 and will result in an
     *    exception at execution time if used.
     *
53 54 55 56 57 58 59 60 61 62
     *  * upsert (boolean): When true, a new document is created if no document
     *    matches the query. The default is false.
     *
     *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
     *
     * @param string       $databaseName   Database name
     * @param string       $collectionName Collection name
     * @param array|object $filter         Query by which to filter documents
     * @param array|object $update         Update to apply to the matched documents
     * @param array        $options        Command options
63
     * @throws InvalidArgumentException for parameter/option parsing errors
64
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
65
    public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
66 67
    {
        if ( ! is_array($update) && ! is_object($update)) {
68
            throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
69 70 71 72 73 74 75 76 77 78 79
        }

        if ( ! \MongoDB\is_first_key_operator($update)) {
            throw new InvalidArgumentException('First key in $update argument is not an update operator');
        }

        $this->update = new Update(
            $databaseName,
            $collectionName,
            $filter,
            $update,
Jeremy Mikola's avatar
Jeremy Mikola committed
80
            ['multi' => true] + $options
81 82 83 84 85 86 87 88 89
        );
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @param Server $server
     * @return UpdateResult
90
     * @throws UnsupportedException if collation is used and unsupported
91
     * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
92 93 94 95 96 97
     */
    public function execute(Server $server)
    {
        return $this->update->execute($server);
    }
}