limit() public method

Limits the number of results returned
public limit ( integer $num ) : MongoCursor
$num integer The number of results to return.
return MongoCursor Returns this cursor
Esempio n. 1
0
 /**
  * The cursor constructor
  * @param string|EMongoDocument $modelClass - The class name for the active record
  * @param array|MongoCursor|EMongoCriteria $criteria -  Either a condition array (without sort,limit and skip) or a MongoCursor Object
  * @param array $fields
  */
 public function __construct($modelClass, $criteria = array(), $fields = array())
 {
     // If $fields has something in it
     if (!empty($fields)) {
         $this->partial = true;
     }
     if (is_string($modelClass)) {
         $this->modelClass = $modelClass;
         $this->model = EMongoDocument::model($this->modelClass);
     } elseif ($modelClass instanceof EMongoDocument) {
         $this->modelClass = get_class($modelClass);
         $this->model = $modelClass;
     }
     if ($criteria instanceof MongoCursor) {
         $this->cursor = $criteria;
         $this->cursor->reset();
     } elseif ($criteria instanceof EMongoCriteria) {
         $this->criteria = $criteria;
         $this->cursor = $this->model->getCollection()->find($criteria->condition, $criteria->project)->sort($criteria->sort);
         if ($criteria->skip > 0) {
             $this->cursor->skip($criteria->skip);
         }
         if ($criteria->limit > 0) {
             $this->cursor->limit($criteria->limit);
         }
     } else {
         // Then we are doing an active query
         $this->criteria = $criteria;
         $this->cursor = $this->model->getCollection()->find($criteria, $fields);
     }
 }
Esempio n. 2
0
 /**
  * Returns an array of items for a page.
  *
  * @param  int   $offset           Page offset
  * @param  int   $itemCountPerPage Number of items per page
  * @return array
  */
 public function getItems($offset, $itemCountPerPage)
 {
     //      $cursor = clone $this -> cursor;
     $this->cursor->skip($offset);
     $this->cursor->limit($itemCountPerPage);
     $resultSet = clone $this->resultSetPrototype;
     $resultSet->initialize($this->createResult($this->cursor));
     return $resultSet;
 }
 /**
  * @param \MongoCursor $cursor
  * @param ODM          $odm
  * @param string|null  $class
  * @param array        $sort
  * @param int          $limit
  * @param int          $offset
  */
 public function __construct(\MongoCursor $cursor, ODM $odm, $class, array $sort = [], $limit = null, $offset = null)
 {
     $this->cursor = $cursor;
     $this->odm = $odm;
     $this->class = $class;
     !empty($sort) && $this->cursor->sort($sort);
     !empty($limit) && $this->cursor->limit($limit);
     !empty($offset) && $this->cursor->skip($offset);
 }
Esempio n. 4
0
 public function limit($itemCountPerPage)
 {
     if ($this->resource instanceof \MongoCursor) {
         return $this->resource->limit($itemCountPerPage);
     }
     return $this->resource;
 }
 /**
  * Limit results to the specified number
  *
  * @since v1.0.0
  */
 public function limit($x = FALSE)
 {
     if ($x !== FALSE && is_numeric($x) && $x >= 1) {
         return $this->_cursor->limit((int) $x);
     }
     return $this->_cursor;
 }
Esempio n. 6
0
 /**
  * @see CActiveDataProvider::fetchData()
  * @return array
  */
 public function fetchData()
 {
     $criteria = $this->getCriteria();
     // I have not refactored this line considering that the condition may have changed from total item count to here, maybe.
     $this->_builder = new EMongoQueryBuilder($this->model, isset($criteria['condition']) && is_array($criteria['condition']) ? $criteria['condition'] : [], isset($criteria['project']) && !empty($criteria['project']) ? $criteria['project'] : []);
     $this->options['condition'] = isset($criteria['condition']) && is_array($criteria['condition']) ? $criteria['condition'] : [];
     $this->options['projection'] = isset($criteria['project']) && !empty($criteria['project']) ? $criteria['project'] : [];
     // If we have sort and limit and skip setup within the incoming criteria let's set it
     if (isset($criteria['sort']) && is_array($criteria['sort'])) {
         $this->_builder->sort($criteria['sort']);
     }
     if (isset($criteria['skip']) && is_int($criteria['skip'])) {
         $this->_builder->skip($criteria['skip']);
     }
     if (isset($criteria['limit']) && is_int($criteria['limit'])) {
         $this->_builder->limit($criteria['limit']);
     }
     if (isset($criteria['hint']) && (is_array($criteria['hint']) || is_string($criteria['hint']))) {
         $this->_builder->hint($criteria['hint']);
     }
     if (($pagination = $this->getPagination()) !== false) {
         $pagination->setItemCount($this->getTotalItemCount());
         $this->_builder->limit($pagination->getLimit());
         $this->_builder->skip($pagination->getOffset());
     }
     if (($sort = $this->getSort()) !== false) {
         $sort = $sort->getOrderBy();
         if (count($sort) > 0) {
             $this->_builder->sort($sort);
         }
     }
     //                var_dump(iterator_to_array($this->_builder->find()));die;
     return $this->_builder->queryAll(true);
 }
Esempio n. 7
0
 /**
  * @see CActiveDataProvider::fetchData()
  * @return array
  */
 public function fetchData()
 {
     $criteria = $this->getCriteria();
     // I have not refactored this line considering that the condition may have changed from total item count to here, maybe.
     $this->_cursor = $this->model->find(isset($criteria['condition']) && is_array($criteria['condition']) ? $criteria['condition'] : array(), isset($criteria['project']) && !empty($criteria['project']) ? $criteria['project'] : array());
     // If we have sort and limit and skip setup within the incoming criteria let's set it
     if (isset($criteria['sort']) && is_array($criteria['sort'])) {
         $this->_cursor->sort($criteria['sort']);
     }
     if (isset($criteria['skip']) && is_int($criteria['skip'])) {
         $this->_cursor->skip($criteria['skip']);
     }
     if (isset($criteria['limit']) && is_int($criteria['limit'])) {
         $this->_cursor->limit($criteria['limit']);
     }
     if (isset($criteria['hint']) && (is_array($criteria['hint']) || is_string($criteria['hint']))) {
         $this->_cursor->hint($criteria['hint']);
     }
     if (($pagination = $this->getPagination()) !== false) {
         $pagination->setItemCount($this->getTotalItemCount());
         $this->_cursor->limit($pagination->getLimit());
         $this->_cursor->skip($pagination->getOffset());
     }
     if (($sort = $this->getSort()) !== false) {
         $sort = $sort->getOrderBy();
         if (count($sort) > 0) {
             $this->_cursor->sort($sort);
         }
     }
     return iterator_to_array($this->_cursor, false);
 }
Esempio n. 8
0
 /**
  * Apply the limit,skip,sort options to the cursor before iterating on documents
  */
 protected function _apply_cursor_controls()
 {
     if ($this->_Cursor) {
         if ($this->_cursor_controls['limit'] !== null) {
             $this->_Cursor->limit($this->_cursor_controls['limit']);
         }
         if ($this->_cursor_controls['skip'] !== null) {
             $this->_Cursor->skip($this->_cursor_controls['skip']);
         }
         if (!empty($this->_cursor_controls['sort'])) {
             $this->_Cursor->sort($this->_cursor_controls['sort']);
         }
     }
 }
 public function configureCursor()
 {
     if ($this->cursor === null) {
         return;
     }
     if ($this->getOffset() > -1) {
         $this->cursor->skip($this->getOffset());
     }
     if ($this->getLimit() > -1) {
         $this->cursor->limit($this->getLimit());
     }
     if ($this->getOrder() !== null) {
         $this->cursor->sort($this->getOrder());
     }
 }
Esempio n. 10
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);
     }
 }
Esempio n. 11
0
 /**
  * Create MongoCursor from string query log
  *
  * @param string $queryString
  * @return \MongoCursor|null
  */
 protected function getCursorFromQueryLog($queryString)
 {
     $cursor = null;
     $connection = $this->panel->getDb();
     $connection->open();
     if ($connection->isActive) {
         $queryInfo = Json::decode($queryString);
         $query = $this->prepareQuery(isset($queryInfo['query']['$query']) ? $queryInfo['query']['$query'] : $queryInfo['query']);
         $cursor = new \MongoCursor($connection->mongoClient, $queryInfo['ns'], $query, $queryInfo['fields']);
         $cursor->limit($queryInfo['limit']);
         $cursor->skip($queryInfo['skip']);
         if (isset($queryInfo['query']['$orderby'])) {
             $cursor->sort($queryInfo['query']['$orderby']);
         }
     }
     return $cursor;
 }
Esempio n. 12
0
 /**
  * {@inheritDoc}
  */
 public function getCursor()
 {
     if (is_null($this->cursor)) {
         $rawCollection = $this->connection->getRaw()->{$this->collection->getName()};
         if (empty($this->criteria)) {
             $this->cursor = $rawCollection->find();
         } else {
             $this->cursor = $rawCollection->find($this->criteria);
         }
         if (isset($this->sorts)) {
             $this->cursor->sort($this->sorts);
         }
         if (isset($this->skip)) {
             $this->cursor->skip($this->skip);
         }
         if (isset($this->limit)) {
             $this->cursor->limit($this->limit);
         }
     }
     return $this->cursor;
 }
Esempio n. 13
0
 /**
  *
  * @return \MongoCursor
  */
 private function getCursor()
 {
     if ($this->cursor) {
         return $this->cursor;
     }
     $this->cursor = $this->collection->getMongoCollection()->find($this->expression->toArray(), $this->fields);
     if ($this->skip) {
         $this->cursor->skip($this->skip);
     }
     if ($this->limit) {
         $this->cursor->limit($this->limit);
     }
     if ($this->options['batchSize']) {
         $this->cursor->batchSize($this->options['batchSize']);
     }
     if ($this->options['clientTimeout']) {
         $this->cursor->timeout($this->options['clientTimeout']);
     }
     if ($this->options['serverTimeout']) {
         $this->cursor->maxTimeMS($this->options['clientTimeout']);
     }
     if ($this->sort) {
         $this->cursor->sort($this->sort);
     }
     if ($this->hint) {
         $this->cursor->hint($this->hint);
     }
     // log request
     if ($this->client->hasLogger()) {
         $this->client->getLogger()->debug(get_called_class() . ': ' . json_encode(array('collection' => $this->collection->getName(), 'query' => $this->expression->toArray(), 'project' => $this->fields, 'sort' => $this->sort)));
     }
     $this->cursor->rewind();
     // define read preferences
     if ($this->readPreference) {
         $this->cursor->setReadPreference($this->readPreference['type'], $this->readPreference['tagsets']);
     }
     return $this->cursor;
 }
Esempio n. 14
0
 /**
  * Apply a limit to this cursor
  * {@see http://www.php.net/manual/en/mongocursor.limit.php}
  * @param integer $limit new limit
  * @since v1.3.4
  */
 public function limit($limit)
 {
     $this->_cursor->limit($limit);
 }
Esempio n. 15
0
 public function limit($limit)
 {
     parent::limit($limit);
     return $this;
 }
Esempio n. 16
0
 /**
  * @param  $amount
  * @return Result
  *
  */
 public function limit($amount)
 {
     $this->myResultCursor = $this->myResultCursor->limit($amount);
     return $this;
 }
Esempio n. 17
0
 public function limit($num)
 {
     parent::limit($num);
     return $this;
 }
Esempio n. 18
0
 /**
  * Limits the number of results returned
  * @link http://www.php.net/manual/en/mongocursor.limit.php
  * @param int $num The number of results to return.
  * @throws MongoCursorException
  * @return $this Returns this cursor
  */
 public function limit($num)
 {
     $this->cursor->limit($num);
     return $this;
 }
Esempio n. 19
0
 /**
  * Applies options to cursor.
  *
  * @param \MongoCursor $cursor
  * @param array        $options
  *
  * @return \MongoCursor
  */
 private function applyOptionsToMongoCursor(\MongoCursor &$cursor, $options = [])
 {
     if (isset($options['limit']['limit'])) {
         $cursor->limit($options['limit']['limit']);
     }
     if (isset($options['limit']['offset'])) {
         $cursor->skip($options['limit']['offset']);
     }
     if (isset($options['sort'])) {
         $cursor->sort($options['sort']);
     }
     return $cursor;
 }
Esempio n. 20
0
 /**
  * Performs limit on MongoCursor
  *
  * @param int $num
  * @return Mapper
  */
 public function limit($num)
 {
     $this->_checkCursor();
     $this->_cursor->limit($num);
     return $this;
 }
 /**
  * Lets you view data
  * 
  * @param Array $criteria the conditions for the data to be viewed
  * @param Array $sort what values you want to sort by
  * @param int $limit how many documents to return back
  * @param Array $fields what fields you want to return back
  * @return Array all the documents back as associative arrays 
  */
 public function view($criteria = array(), $sort = array(), $limit = 100, $fields = array())
 {
     ValidatorsUtil::isNullOrEmpty($this->_mongoCollection, "Mongo collection isn't valid, have you set a collection?");
     $this->_mongoCursor = $this->_mongoCollection->find($criteria, $fields);
     if (is_array($sort) && count($sort) != 0) {
         $this->_mongoCursor->sort($sort);
     }
     if (is_numeric($limit)) {
         $this->_mongoCursor->limit($limit);
     }
     $counter = 0;
     $this->_documents = array();
     foreach ($this->_mongoCursor as $document) {
         $this->_documents[$counter] = $document;
         $counter++;
     }
     if ($counter == 0) {
         $this->_documents = array();
     }
     $this->_count = $counter;
     $this->_id = "";
     return $this->_documents;
 }
Esempio n. 22
0
 /**
  * {@inheritDoc}
  *
  * @api
  */
 public function getItems($offset, $limit)
 {
     $this->cursor->skip($offset);
     $this->cursor->limit($limit);
     return iterator_to_array($this->cursor);
 }