/**
  * 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) ? 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) : [];
             $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) {
         ExceptionConverter::toLegacy($e, 'MongoResultException');
     }
     if ($document) {
         $document = TypeConverter::toLegacy($document);
     }
     return $document;
 }
Example #2
0
 /**
  * Updates a document and returns it.
  * @param array $condition query condition
  * @param array $update update criteria
  * @param array $fields fields to be returned
  * @param array $options list of options in format: optionName => optionValue.
  * @return array|null the original document, or the modified document when $options['new'] is set.
  * @throws Exception on failure.
  * @see http://www.php.net/manual/en/mongocollection.findandmodify.php
  */
 public function findAndModify($condition, $update, $options = [])
 {
     $token = "mongoyii\\" . $this->collection->getCollectionName() . ".findAndModify({\$condition: " . json_encode($condition) . ", \$update: " . json_encode($update) . ", \$options: " . json_encode($options) . " })";
     Yii::trace($token, "mongoyii\\Collection");
     if ($this->client->enableProfiling) {
         Yii::beginProfile($token, 'mongoyii\\Collection.findAndModify');
     }
     try {
         if (isset($options['remove']) && $options['remove']) {
             $result = $this->collection->findOneAndDelete($condition, $options);
         } elseif (isset($options['update']) && $options['update']) {
             if (isset($options['upsert']) && $options['upsert']) {
                 $result = $this->collection->findOneAndReplace($condition, $update, $options);
             } else {
                 $result = $this->collection->findOneAndUpdate($condition, $update, $options);
             }
         } else {
             throw new Exception('Must enter a operation type for findAndModify');
         }
         if ($this->client->enableProfiling) {
             Yii::endProfile($token, 'mongoyii\\Collection.findAndModify');
         }
         return $result;
     } catch (\Exception $e) {
         if ($this->client->enableProfiling) {
             Yii::endProfile($token, 'mongoyii\\Collection.findAndModify');
         }
         throw new Exception($e->getMessage(), (int) $e->getCode(), $e);
     }
 }
 /**
  * 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);
     if (isset($options['remove'])) {
         unset($options['remove']);
         $document = $this->collection->findOneAndDelete($query, $options);
     } else {
         $update = is_array($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) : [];
         $document = $this->collection->findOneAndUpdate($query, $update, $options);
     }
     if ($document) {
         $document = TypeConverter::toLegacy($document);
     }
     return $document;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function findOneAndDelete($filter, array $options = [])
 {
     $event = $this->startQueryLogging(__FUNCTION__, $filter, null, $options);
     $result = parent::findOneAndDelete($filter, $options);
     $this->logger->logQuery($event);
     return $result;
 }
Example #5
0
 /**
  * @param string $identity
  * @return void
  */
 public function delete(string $identity)
 {
     $this->collection->findOneAndDelete(['identity' => $identity]);
 }