Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function findBy(array $criteria)
 {
     $models = [];
     foreach ($this->collection->find($criteria) as $values) {
         $models[] = $this->hydrate($values);
     }
     return $models;
 }
Пример #2
0
 /**
  * Apply update from the provided products documents into MongoDB
  *
  * @param array $docs
  */
 protected function updateDocuments(array $docs)
 {
     foreach ($docs as $doc) {
         $id = $doc['_id'];
         $this->collection->update(['_id' => $id], $doc);
     }
 }
 /**
  * Apply update from the provided products documents into MongoDB
  *
  * @param array $docs
  */
 protected function updateDocuments($docs)
 {
     foreach ($docs as $doc) {
         $criteria = ['_id' => $doc['_id']];
         $this->collection->update($criteria, $doc);
     }
 }
 /**
  * @return ArrayIterator
  * @throws \Exception|\MongoException
  * @throws \RuntimeException
  */
 public function aggregate()
 {
     $database = $this->collection->getDatabase();
     $command = array();
     $command['aggregate'] = $this->collection->getName();
     $command['pipeline'] = $this->getPipeline();
     try {
         $result = $database->command($command);
     } catch (\MongoException $e) {
         throw $e;
     }
     if (!$result['ok']) {
         throw new \RuntimeException($result['errmsg']);
     }
     return new ArrayIterator(isset($result['result']) ? $result['result'] : array());
 }
 /**
  * Apply update from the provided products documents into MongoDB
  *
  * @param array $docs
  */
 protected function updateDocuments($docs)
 {
     foreach ($docs as $doc) {
         $criteria = ['_id' => $doc['_id']];
         $this->collection->update($criteria, $doc);
         $this->stepExecution->incrementSummaryInfo('process');
     }
 }
Пример #6
0
 /**
  * Constructor.
  *
  * @param Database         $database        Database to which this collection belongs
  * @param \MongoCollection $mongoCollection MongoCollection instance being wrapped
  * @param EventManager     $evm             EventManager instance
  * @param integer          $numRetries      Number of times to retry queries
  * @param callable         $loggerCallable  The logger callable
  */
 public function __construct(Database $database, \MongoCollection $mongoCollection, EventManager $evm, $numRetries, $loggerCallable)
 {
     if (!is_callable($loggerCallable)) {
         throw new \InvalidArgumentException('$loggerCallable must be a valid callback');
     }
     $this->loggerCallable = $loggerCallable;
     parent::__construct($database, $mongoCollection, $evm, $numRetries);
 }
Пример #7
0
 public function execute()
 {
     switch ($this->query['type']) {
         case self::TYPE_FIND:
             if (isset($this->query['mapReduce']['reduce'])) {
                 $this->query['query'][$this->cmd . 'where'] = $this->query['mapReduce']['reduce'];
             }
             $cursor = $this->collection->find($this->query['query'], $this->query['select']);
             $this->prepareCursor($cursor);
             return $cursor;
         case self::TYPE_FIND_AND_UPDATE:
             if ($this->query['sort']) {
                 $this->options['sort'] = $this->query['sort'];
             }
             if ($this->query['select']) {
                 $this->options['fields'] = $this->query['select'];
             }
             if ($this->query['upsert']) {
                 $this->options['upsert'] = true;
             }
             if ($this->query['new']) {
                 $this->options['new'] = true;
             }
             return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], $this->options);
         case self::TYPE_FIND_AND_REMOVE:
             if ($this->query['sort']) {
                 $this->options['sort'] = $this->query['sort'];
             }
             if ($this->query['select']) {
                 $this->options['fields'] = $this->query['select'];
             }
             return $this->collection->findAndRemove($this->query['query'], $this->options);
         case self::TYPE_INSERT:
             return $this->collection->insert($this->query['newObj']);
         case self::TYPE_UPDATE:
             return $this->collection->update($this->query['query'], $this->query['newObj'], $this->options);
         case self::TYPE_REMOVE:
             return $this->collection->remove($this->query['query'], $this->options);
         case self::TYPE_GROUP:
             return $this->collection->group($this->query['group']['keys'], $this->query['group']['initial'], $this->query['mapReduce']['reduce'], $this->query['query']);
         case self::TYPE_MAP_REDUCE:
             if (!isset($this->query['mapReduce']['out'])) {
                 $this->query['mapReduce']['out'] = array('inline' => true);
             }
             $cursor = $this->collection->mapReduce($this->query['mapReduce']['map'], $this->query['mapReduce']['reduce'], $this->query['mapReduce']['out'], $this->query['query'], $this->options);
             if ($cursor instanceof Cursor) {
                 $this->prepareCursor($cursor);
             }
             return $cursor;
         case self::TYPE_DISTINCT_FIELD:
             return $this->collection->distinct($this->query['distinctField'], $this->query['query'], $this->options);
         case self::TYPE_GEO_LOCATION:
             if (isset($this->query['limit']) && $this->query['limit']) {
                 $this->options['num'] = $this->query['limit'];
             }
             return $this->collection->near($this->query['near'], $this->query['query'], $this->options);
     }
 }
Пример #8
0
 /**
  * Recreates the internal MongoCursor.
  */
 public function recreate()
 {
     $this->mongoCursor = $this->collection->getMongoCollection()->find($this->query, $this->fields);
     if ($this->hint !== null) {
         $this->mongoCursor->hint($this->hint);
     }
     if ($this->immortal !== null) {
         $this->mongoCursor->immortal($this->immortal);
     }
     foreach ($this->options as $key => $value) {
         $this->mongoCursor->addOption($key, $value);
     }
     if ($this->batchSize !== null) {
         $this->mongoCursor->batchSize($this->batchSize);
     }
     if ($this->limit !== null) {
         $this->mongoCursor->limit($this->limit);
     }
     if ($this->skip !== null) {
         $this->mongoCursor->skip($this->skip);
     }
     if ($this->slaveOkay !== null) {
         $this->setMongoCursorSlaveOkay($this->slaveOkay);
     }
     // Set read preferences after slaveOkay, since they may be more specific
     if ($this->readPreference !== null) {
         if ($this->readPreferenceTags !== null) {
             $this->mongoCursor->setReadPreference($this->readPreference, $this->readPreferenceTags);
         } else {
             $this->mongoCursor->setReadPreference($this->readPreference);
         }
     }
     if ($this->snapshot) {
         $this->mongoCursor->snapshot();
     }
     if ($this->sort !== null) {
         $this->mongoCursor->sort($this->sort);
     }
     if ($this->tailable !== null) {
         $this->mongoCursor->tailable($this->tailable);
     }
     if ($this->timeout !== null) {
         $this->mongoCursor->timeout($this->timeout);
     }
     if ($this->maxTimeMS !== null) {
         $this->mongoCursor->addOption('$maxTimeMS', $this->maxTimeMS);
     }
 }
Пример #9
0
 /**
  * @return array
  *
  * @throws \Exception
  */
 protected function getCollectionStats()
 {
     $database = $this->collection->getDatabase();
     $name = $this->collection->getName();
     try {
         if (!($data = $database->command(array('collStats' => $name)))) {
             $message = sprintf("Statistics not found for collection %s", $name);
             throw new Exception($message);
         }
         if (isset($data['errmsg'])) {
             throw new Exception($data['errmsg']);
         }
         return $data;
     } catch (Exception $e) {
         throw $e;
     }
     return array();
 }
Пример #10
0
 /**
  * @see Collection::validate()
  */
 public function validate($scanData = false)
 {
     $this->log(['validate' => true, 'scanData' => $scanData]);
     return parent::validate($scanData);
 }
Пример #11
0
 /**
  * Executes a closure with a temporary read preference on a database or
  * collection.
  *
  * @param Database|Collection $object
  * @param \Closure            $closure
  * @return mixed
  */
 private function withReadPreference($object, \Closure $closure)
 {
     if (!isset($this->query['readPreference'])) {
         return $closure();
     }
     $prevReadPref = $object->getReadPreference();
     $object->setReadPreference($this->query['readPreference'], $this->query['readPreferenceTags']);
     try {
         $result = $closure();
     } catch (\Exception $e) {
     }
     $prevTags = !empty($prevReadPref['tagsets']) ? $prevReadPref['tagsets'] : null;
     $object->setReadPreference($prevReadPref['type'], $prevTags);
     if (isset($e)) {
         throw $e;
     }
     return $result;
 }
Пример #12
0
 /**
  * Executes the aggregation pipeline
  *
  * @param array $options
  * @return Iterator
  */
 public function execute($options = array())
 {
     return $this->collection->aggregate($this->getPipeline(), $options);
 }
Пример #13
0
 private function query(array $query, array $newObj)
 {
     $query = $this->persister->prepareQueryOrNewObj($query);
     $result = $this->collection->update($query, $newObj, array('multiple' => true));
     return $result;
 }
Пример #14
0
 /**
  * @return string[]
  */
 public function findIdentities()
 {
     return $this->collection->distinct($this->identityField)->toArray();
 }
Пример #15
0
 /**
  * {@inheritdoc}
  */
 public function retrieve($id)
 {
     return $this->formatResponse($this->collection->findOne(['_id' => $this->convertId($id)]));
 }
Пример #16
0
 public function execute()
 {
     switch ($this->query['type']) {
         case self::TYPE_FIND:
             $cursor = $this->collection->find($this->query['query'], $this->query['select']);
             return $this->prepareCursor($cursor);
         case self::TYPE_FIND_AND_UPDATE:
             if ($this->query['sort']) {
                 $this->options['sort'] = $this->query['sort'];
             }
             if ($this->query['select']) {
                 $this->options['fields'] = $this->query['select'];
             }
             if ($this->query['upsert']) {
                 $this->options['upsert'] = true;
             }
             if ($this->query['new']) {
                 $this->options['new'] = true;
             }
             return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], $this->options);
         case self::TYPE_FIND_AND_REMOVE:
             if ($this->query['sort']) {
                 $this->options['sort'] = $this->query['sort'];
             }
             if ($this->query['select']) {
                 $this->options['fields'] = $this->query['select'];
             }
             return $this->collection->findAndRemove($this->query['query'], $this->options);
         case self::TYPE_INSERT:
             return $this->collection->insert($this->query['newObj']);
         case self::TYPE_UPDATE:
             if ($this->query['upsert']) {
                 $this->options['upsert'] = $this->query['upsert'];
             }
             if ($this->query['multiple']) {
                 $this->options['multiple'] = $this->query['multiple'];
             }
             return $this->collection->update($this->query['query'], $this->query['newObj'], $this->options);
         case self::TYPE_REMOVE:
             return $this->collection->remove($this->query['query'], $this->options);
         case self::TYPE_GROUP:
             if (!empty($this->query['query'])) {
                 $this->query['group']['options']['condition'] = $this->query['query'];
             }
             $options = array_merge($this->options, $this->query['group']['options']);
             return $this->collection->group($this->query['group']['keys'], $this->query['group']['initial'], $this->query['group']['reduce'], $options);
         case self::TYPE_MAP_REDUCE:
             if (!isset($this->query['mapReduce']['out'])) {
                 $this->query['mapReduce']['out'] = array('inline' => true);
             }
             $options = array_merge($this->options, $this->query['mapReduce']['options']);
             $cursor = $this->collection->mapReduce($this->query['mapReduce']['map'], $this->query['mapReduce']['reduce'], $this->query['mapReduce']['out'], $this->query['query'], $options);
             if ($cursor instanceof Cursor) {
                 $cursor = $this->prepareCursor($cursor);
             }
             return $cursor;
         case self::TYPE_DISTINCT_FIELD:
             return $this->collection->distinct($this->query['distinctField'], $this->query['query'], $this->options);
         case self::TYPE_GEO_LOCATION:
             if (isset($this->query['limit']) && $this->query['limit']) {
                 $this->options['num'] = $this->query['limit'];
             }
             foreach (array('distanceMultiplier', 'maxDistance', 'spherical') as $key) {
                 if (isset($this->query['geoNear'][$key]) && $this->query['geoNear'][$key]) {
                     $this->options[$key] = $this->query['geoNear'][$key];
                 }
             }
             return $this->collection->near($this->query['geoNear']['near'], $this->query['query'], $this->options);
         case self::TYPE_COUNT:
             return $this->collection->count($this->query['query']);
     }
 }
Пример #17
0
 /**
  * Execute the insert query and persist the GridFSFile if necessary.
  *
  * @see Collection::doInsert()
  * @param array $a
  * @param array $options
  * @return mixed
  */
 protected function doInsert(array &$a, array $options = [])
 {
     // If there is no file, perform a basic insertion
     if (!isset($a['file'])) {
         parent::doInsert($a, $options);
         return;
     }
     /* If the file is dirty (i.e. it must be persisted), delegate to the
      * storeFile() method. Otherwise, perform a basic insertion.
      */
     $file = $a['file'];
     // instanceof GridFSFile
     unset($a['file']);
     if ($file->isDirty()) {
         $this->storeFile($file, $a, $options);
     } else {
         parent::doInsert($a, $options);
     }
     $a['file'] = $file;
     return $a;
 }