/** * Constructs a update command. * * Supported options: * * * multi (boolean): When true, updates all documents matching the query. * This option cannot be true if the $update argument is a replacement * document (i.e. contains no update operators). The default is false. * * * 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 delete documents * @param array|object $update Update to apply to the matched * document(s) or a replacement document * @param array $options Command options * @throws InvalidArgumentException */ public function __construct($databaseName, $collectionName, $filter, $update, array $options = array()) { if (!is_array($filter) && !is_object($filter)) { throw new InvalidArgumentTypeException('$filter', $filter, 'array or object'); } if (!is_array($update) && !is_object($update)) { throw new InvalidArgumentTypeException('$update', $filter, 'array or object'); } $options += array('multi' => false, 'upsert' => false); if (!is_bool($options['multi'])) { throw new InvalidArgumentTypeException('"multi" option', $options['multi'], 'boolean'); } if ($options['multi'] && !\MongoDB\is_first_key_operator($update)) { throw new InvalidArgumentException('"multi" option cannot be true if $update is a replacement document'); } if (!is_bool($options['upsert'])) { throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean'); } if (isset($options['writeConcern']) && !$options['writeConcern'] instanceof WriteConcern) { throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\\Driver\\WriteConcern'); } $this->databaseName = (string) $databaseName; $this->collectionName = (string) $collectionName; $this->filter = $filter; $this->update = $update; $this->options = $options; }
/** * Constructs a findAndModify command for replacing a document. * * Supported options: * * * bypassDocumentValidation (boolean): If true, allows the write to opt * out of document level validation. * * * maxTimeMS (integer): The maximum amount of time to allow the query to * run. * * * projection (document): Limits the fields to return for the matching * document. * * * returnDocument (enum): Whether to return the document before or after * the update is applied. Must be either RETURN_DOCUMENT_BEFORE or * RETURN_DOCUMENT_AFTER. The default is RETURN_DOCUMENT_BEFORE. * * * sort (document): Determines which document the operation modifies if * the query selects multiple documents. * * * 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. This option * is only supported for server versions >= 3.2. * * @param string $databaseName Database name * @param string $collectionName Collection name * @param array|object $filter Query by which to filter documents * @param array|object $replacement Replacement document * @param array $options Command options * @throws InvalidArgumentException */ public function __construct($databaseName, $collectionName, $filter, $replacement, array $options = []) { if (!is_array($filter) && !is_object($filter)) { throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object'); } if (!is_array($replacement) && !is_object($replacement)) { throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object'); } if (\MongoDB\is_first_key_operator($replacement)) { throw new InvalidArgumentException('First key in $replacement argument is an update operator'); } $options += ['returnDocument' => self::RETURN_DOCUMENT_BEFORE, 'upsert' => false]; if (isset($options['projection']) && !is_array($options['projection']) && !is_object($options['projection'])) { throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object'); } if (!is_integer($options['returnDocument'])) { throw InvalidArgumentException::invalidType('"returnDocument" option', $options['returnDocument'], 'integer'); } if ($options['returnDocument'] !== self::RETURN_DOCUMENT_AFTER && $options['returnDocument'] !== self::RETURN_DOCUMENT_BEFORE) { throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']); } if (isset($options['projection'])) { $options['fields'] = $options['projection']; } $options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER; unset($options['projection'], $options['returnDocument']); $this->findAndModify = new FindAndModify($databaseName, $collectionName, ['query' => $filter, 'update' => $replacement] + $options); }
/** * Constructs a findAndModify command for updating a document. * * Supported options: * * * maxTimeMS (integer): The maximum amount of time to allow the query to * run. * * * projection (document): Limits the fields to return for the matching * document. * * * returnDocument (enum): Whether to return the document before or after * the update is applied. Must be either RETURN_DOCUMENT_BEFORE or * RETURN_DOCUMENT_AFTER. The default is RETURN_DOCUMENT_BEFORE. * * * sort (document): Determines which document the operation modifies if * the query selects multiple documents. * * * upsert (boolean): When true, a new document is created if no document * matches the query. The default is false. * * @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 document * @param array $options Command options * @throws InvalidArgumentException */ public function __construct($databaseName, $collectionName, $filter, $update, array $options = array()) { if (!is_array($filter) && !is_object($filter)) { throw new InvalidArgumentTypeException('$filter', $filter, 'array or object'); } if (!is_array($update) && !is_object($update)) { throw new InvalidArgumentTypeException('$update', $update, 'array or object'); } if (!\MongoDB\is_first_key_operator($update)) { throw new InvalidArgumentException('First key in $update argument is not an update operator'); } $options += array('returnDocument' => self::RETURN_DOCUMENT_BEFORE, 'upsert' => false); if (isset($options['maxTimeMS']) && !is_integer($options['maxTimeMS'])) { throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer'); } if (isset($options['projection']) && !is_array($options['projection']) && !is_object($options['projection'])) { throw new InvalidArgumentTypeException('"projection" option', $options['projection'], 'array or object'); } if (!is_integer($options['returnDocument'])) { throw new InvalidArgumentTypeException('"returnDocument" option', $options['returnDocument'], 'integer'); } if ($options['returnDocument'] !== self::RETURN_DOCUMENT_AFTER && $options['returnDocument'] !== self::RETURN_DOCUMENT_BEFORE) { throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']); } if (isset($options['sort']) && !is_array($options['sort']) && !is_object($options['sort'])) { throw new InvalidArgumentTypeException('"sort" option', $options['sort'], 'array or object'); } if (!is_bool($options['upsert'])) { throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean'); } $this->findAndModify = new FindAndModify($databaseName, $collectionName, array('fields' => isset($options['projection']) ? $options['projection'] : null, 'maxTimeMS' => isset($options['maxTimeMS']) ? $options['maxTimeMS'] : null, 'new' => $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER, 'query' => $filter, 'sort' => isset($options['sort']) ? $options['sort'] : null, 'update' => $update, 'upsert' => $options['upsert'])); }
public function findAndModify($query, array $update = [], array $fields = [], array $options = []) { if (!\MongoDB\is_first_key_operator($update)) { $update = ['$set' => $update]; } $doc = (array) $this->collection->findOneAndUpdate($query, $update, array_merge($options, ['typeMap' => static::$TYPE_MAP])); $doc['_id'] = new MongoId($doc['_id']); return $doc; }
/** * Constructs an update command. * * Supported options: * * * 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 * @throws InvalidArgumentException */ public function __construct($databaseName, $collectionName, $filter, $update, array $options = []) { if (!is_array($update) && !is_object($update)) { throw new InvalidArgumentTypeException('$update', $update, 'array or object'); } 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, ['multi' => true] + $options); }
/** * Constructs an update command. * * Supported options: * * * 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 $replacement Replacement document * @param array $options Command options * @throws InvalidArgumentException */ public function __construct($databaseName, $collectionName, $filter, $replacement, array $options = []) { if (!is_array($replacement) && !is_object($replacement)) { throw new InvalidArgumentTypeException('$replacement', $replacement, 'array or object'); } if (\MongoDB\is_first_key_operator($replacement)) { throw new InvalidArgumentException('First key in $replacement argument is an update operator'); } $this->update = new Update($databaseName, $collectionName, $filter, $replacement, ['multi' => false] + $options); }
/** * Constructs a bulk write operation. * * Example array structure for all supported operation types: * * [ * [ 'deleteOne' => [ $filter ] ], * [ 'deleteMany' => [ $filter ] ], * [ 'insertOne' => [ $document ] ], * [ 'replaceOne' => [ $filter, $replacement, $options ] ], * [ 'updateMany' => [ $filter, $update, $options ] ], * [ 'updateOne' => [ $filter, $update, $options ] ], * ] * * Arguments correspond to the respective Operation classes; however, the * writeConcern option is specified for the top-level bulk write operation * instead of each individual operations. * * Supported options for replaceOne, updateMany, and updateOne operations: * * * upsert (boolean): When true, a new document is created if no document * matches the query. The default is false. * * Supported options for the bulk write operation: * * * ordered (boolean): If true, when an insert fails, return without * performing the remaining writes. If false, when a write fails, * continue with the remaining writes, if any. The default is true. * * * writeConcern (MongoDB\Driver\WriteConcern): Write concern. * * @param string $databaseName Database name * @param string $collectionName Collection name * @param array[] $operations List of write operations * @param array $options Command options * @throws InvalidArgumentException */ public function __construct($databaseName, $collectionName, array $operations, array $options = []) { if (empty($operations)) { throw new InvalidArgumentException('$operations is empty'); } $expectedIndex = 0; foreach ($operations as $i => $operation) { if ($i !== $expectedIndex) { throw new InvalidArgumentException(sprintf('$operations is not a list (unexpected index: "%s")', $i)); } if (!is_array($operation)) { throw new InvalidArgumentTypeException(sprintf('$operations[%d]', $i), $operation, 'array'); } if (count($operation) !== 1) { throw new InvalidArgumentException(sprintf('Expected one element in $operation[%d], actually: %d', $i, count($operation))); } $type = key($operation); $args = current($operation); if (!isset($args[0]) && !array_key_exists(0, $args)) { throw new InvalidArgumentException(sprintf('Missing first argument for $operations[%d]["%s"]', $i, $type)); } if (!is_array($args[0]) && !is_object($args[0])) { throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0], 'array or object'); } switch ($type) { case self::INSERT_ONE: break; case self::DELETE_MANY: case self::DELETE_ONE: $operations[$i][$type][1] = ['limit' => $type === self::DELETE_ONE ? 1 : 0]; break; case self::REPLACE_ONE: if (!isset($args[1]) && !array_key_exists(1, $args)) { throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type)); } if (!is_array($args[1]) && !is_object($args[1])) { throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object'); } if (\MongoDB\is_first_key_operator($args[1])) { throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is an update operator', $i, $type)); } if (!isset($args[2])) { $args[2] = []; } if (!is_array($args[2])) { throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array'); } $args[2]['multi'] = false; $args[2] += ['upsert' => false]; if (!is_bool($args[2]['upsert'])) { throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean'); } $operations[$i][$type][2] = $args[2]; break; case self::UPDATE_MANY: case self::UPDATE_ONE: if (!isset($args[1]) && !array_key_exists(1, $args)) { throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type)); } if (!is_array($args[1]) && !is_object($args[1])) { throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object'); } if (!\MongoDB\is_first_key_operator($args[1])) { throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is not an update operator', $i, $type)); } if (!isset($args[2])) { $args[2] = []; } if (!is_array($args[2])) { throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array'); } $args[2]['multi'] = $type === self::UPDATE_MANY; $args[2] += ['upsert' => false]; if (!is_bool($args[2]['upsert'])) { throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean'); } $operations[$i][$type][2] = $args[2]; break; default: throw new InvalidArgumentException(sprintf('Unknown operation type "%s" in $operations[%d]', $type, $i)); } $expectedIndex += 1; } $options += ['ordered' => true]; if (!is_bool($options['ordered'])) { throw new InvalidArgumentTypeException('"ordered" option', $options['ordered'], 'boolean'); } if (isset($options['writeConcern']) && !$options['writeConcern'] instanceof WriteConcern) { throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\\Driver\\WriteConcern'); } $this->databaseName = (string) $databaseName; $this->collectionName = (string) $collectionName; $this->operations = $operations; $this->options = $options; }
/** * Update a document and return it * * @link http://www.php.net/manual/ru/mongocollection.findandmodify.php * @param array $query The query criteria to search for. * @param array $update The update criteria. * @param array $fields Optionally only return these fields. * @param array $options An array of options to apply, such as remove the match document from the DB and return it. * @return array Returns the original document, or the modified document when new is set. */ public function findAndModify(array $query, array $update = null, array $fields = null, array $options = []) { $query = TypeConverter::fromLegacy($query); try { if (isset($options['remove'])) { unset($options['remove']); $document = $this->collection->findOneAndDelete($query, $options); } else { $update = is_array($update) ? $update : []; if (isset($options['update']) && is_array($options['update'])) { $update = $options['update']; unset($options['update']); } $update = TypeConverter::fromLegacy($update); if (isset($options['new'])) { $options['returnDocument'] = \MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER; unset($options['new']); } $options['projection'] = is_array($fields) ? TypeConverter::fromLegacy($fields) : []; if (!\MongoDB\is_first_key_operator($update)) { $document = $this->collection->findOneAndReplace($query, $update, $options); } else { $document = $this->collection->findOneAndUpdate($query, $update, $options); } } } catch (\MongoDB\Driver\Exception\ConnectionException $e) { throw new MongoResultException($e->getMessage(), $e->getCode(), $e); } catch (\MongoDB\Driver\Exception\Exception $e) { throw ExceptionConverter::toLegacy($e, 'MongoResultException'); } if ($document) { $document = TypeConverter::toLegacy($document); } return $document; }