UpdateMany.php 3.96 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

namespace MongoDB\Operation;

20
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
21
use MongoDB\Driver\Server;
22
use MongoDB\Exception\InvalidArgumentException;
23
use MongoDB\Exception\UnsupportedException;
24 25 26 27
use MongoDB\UpdateResult;
use function is_array;
use function is_object;
use function MongoDB\is_first_key_operator;
28
use function MongoDB\is_pipeline;
29 30 31 32 33

/**
 * Operation for updating multiple documents with the update command.
 *
 * @api
34
 * @see \MongoDB\Collection::updateMany()
35 36
 * @see http://docs.mongodb.org/manual/reference/command/update/
 */
37
class UpdateMany implements Executable, Explainable
38
{
39
    /** @var Update */
40 41 42 43 44 45 46
    private $update;

    /**
     * Constructs an update command.
     *
     * Supported options:
     *
47 48 49 50 51 52
     *  * arrayFilters (document array): A set of filters specifying to which
     *    array elements an update should apply.
     *
     *    This is not supported for server versions < 3.6 and will result in an$
     *    exception at execution time if used.
     *
53 54 55 56 57
     *  * 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.
58
     *
59 60 61 62 63
     *  * collation (document): Collation specification.
     *
     *    This is not supported for server versions < 3.4 and will result in an
     *    exception at execution time if used.
     *
64 65 66 67
     *  * session (MongoDB\Driver\Session): Client session.
     *
     *    Sessions are not supported for server versions < 3.6.
     *
68 69 70 71 72 73 74 75 76 77
     *  * 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
78
     * @throws InvalidArgumentException for parameter/option parsing errors
79
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
80
    public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
81
    {
82
        if (! is_array($update) && ! is_object($update)) {
83
            throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
84 85
        }

86 87
        if (! is_first_key_operator($update) && ! is_pipeline($update)) {
            throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
88 89 90 91 92 93 94
        }

        $this->update = new Update(
            $databaseName,
            $collectionName,
            $filter,
            $update,
Jeremy Mikola's avatar
Jeremy Mikola committed
95
            ['multi' => true] + $options
96 97 98 99 100 101 102 103 104
        );
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @param Server $server
     * @return UpdateResult
105
     * @throws UnsupportedException if collation is used and unsupported
106
     * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
107 108 109 110 111
     */
    public function execute(Server $server)
    {
        return $this->update->execute($server);
    }
112 113 114 115 116

    public function getCommandDocument(Server $server)
    {
        return $this->update->getCommandDocument($server);
    }
117
}