Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401]
1 <?php 2 /* 3 * Copyright 2015-2017 MongoDB, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 namespace MongoDB\Operation; 19 20 use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; 21 use MongoDB\Driver\Server; 22 use MongoDB\Exception\InvalidArgumentException; 23 use MongoDB\Exception\UnsupportedException; 24 use function is_array; 25 use function is_integer; 26 use function is_object; 27 use function MongoDB\is_first_key_operator; 28 29 /** 30 * Operation for replacing a document with the findAndModify command. 31 * 32 * @api 33 * @see \MongoDB\Collection::findOneAndReplace() 34 * @see http://docs.mongodb.org/manual/reference/command/findAndModify/ 35 */ 36 class FindOneAndReplace implements Executable, Explainable 37 { 38 const RETURN_DOCUMENT_BEFORE = 1; 39 const RETURN_DOCUMENT_AFTER = 2; 40 41 /** @var FindAndModify */ 42 private $findAndModify; 43 44 /** 45 * Constructs a findAndModify command for replacing a document. 46 * 47 * Supported options: 48 * 49 * * bypassDocumentValidation (boolean): If true, allows the write to 50 * circumvent document level validation. 51 * 52 * For servers < 3.2, this option is ignored as document level validation 53 * is not available. 54 * 55 * * collation (document): Collation specification. 56 * 57 * This is not supported for server versions < 3.4 and will result in an 58 * exception at execution time if used. 59 * 60 * * maxTimeMS (integer): The maximum amount of time to allow the query to 61 * run. 62 * 63 * * projection (document): Limits the fields to return for the matching 64 * document. 65 * 66 * * returnDocument (enum): Whether to return the document before or after 67 * the update is applied. Must be either 68 * FindOneAndReplace::RETURN_DOCUMENT_BEFORE or 69 * FindOneAndReplace::RETURN_DOCUMENT_AFTER. The default is 70 * FindOneAndReplace::RETURN_DOCUMENT_BEFORE. 71 * 72 * * session (MongoDB\Driver\Session): Client session. 73 * 74 * Sessions are not supported for server versions < 3.6. 75 * 76 * * sort (document): Determines which document the operation modifies if 77 * the query selects multiple documents. 78 * 79 * * typeMap (array): Type map for BSON deserialization. 80 * 81 * * upsert (boolean): When true, a new document is created if no document 82 * matches the query. The default is false. 83 * 84 * * writeConcern (MongoDB\Driver\WriteConcern): Write concern. 85 * 86 * This is not supported for server versions < 3.2 and will result in an 87 * exception at execution time if used. 88 * 89 * @param string $databaseName Database name 90 * @param string $collectionName Collection name 91 * @param array|object $filter Query by which to filter documents 92 * @param array|object $replacement Replacement document 93 * @param array $options Command options 94 * @throws InvalidArgumentException for parameter/option parsing errors 95 */ 96 public function __construct($databaseName, $collectionName, $filter, $replacement, array $options = []) 97 { 98 if (! is_array($filter) && ! is_object($filter)) { 99 throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object'); 100 } 101 102 if (! is_array($replacement) && ! is_object($replacement)) { 103 throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object'); 104 } 105 106 if (is_first_key_operator($replacement)) { 107 throw new InvalidArgumentException('First key in $replacement argument is an update operator'); 108 } 109 110 $options += [ 111 'returnDocument' => self::RETURN_DOCUMENT_BEFORE, 112 'upsert' => false, 113 ]; 114 115 if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) { 116 throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object'); 117 } 118 119 if (! is_integer($options['returnDocument'])) { 120 throw InvalidArgumentException::invalidType('"returnDocument" option', $options['returnDocument'], 'integer'); 121 } 122 123 if ($options['returnDocument'] !== self::RETURN_DOCUMENT_AFTER && 124 $options['returnDocument'] !== self::RETURN_DOCUMENT_BEFORE) { 125 throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']); 126 } 127 128 if (isset($options['projection'])) { 129 $options['fields'] = $options['projection']; 130 } 131 132 $options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER; 133 134 unset($options['projection'], $options['returnDocument']); 135 136 $this->findAndModify = new FindAndModify( 137 $databaseName, 138 $collectionName, 139 ['query' => $filter, 'update' => $replacement] + $options 140 ); 141 } 142 143 /** 144 * Execute the operation. 145 * 146 * @see Executable::execute() 147 * @param Server $server 148 * @return array|object|null 149 * @throws UnsupportedException if collation or write concern is used and unsupported 150 * @throws DriverRuntimeException for other driver errors (e.g. connection errors) 151 */ 152 public function execute(Server $server) 153 { 154 return $this->findAndModify->execute($server); 155 } 156 157 public function getCommandDocument(Server $server) 158 { 159 return $this->findAndModify->getCommandDocument($server); 160 } 161 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body