lithium\data\source\MongoDb::read()

public method

Read from document

Parameters

  • string $query
  • array $options

Returns

object

Filter

This method can be filtered.

Source

	public function read($query, array $options = []) {
		$this->_checkConnection();

		$defaults = ['return' => 'resource'];
		$options += $defaults;

		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$prefix = $this->_config['gridPrefix'];

			$query = $params['query'];
			$options = $params['options'];
			$args = $query->export($this);
			$source = $args['source'];
			$model = $query->model();

			if ($group = $args['group']) {
				$result = $this->_group($group, $args, $options);
				$config = ['class' => 'set', 'defaults' => false] + compact('query') + $result;
				return $model::create($config['data'], $config);
			}
			$collection = $this->connection->{$source};

			if ($source === "{$prefix}.files") {
				$collection = $this->connection->getGridFS($prefix);
			}
			$result = $collection->find($args['conditions'], $args['fields']);

			if ($query->calculate()) {
				return $result;
			}

			$resource = $result->sort($args['order'])->limit($args['limit'])->skip($args['offset']);
			$result = $this->_instance('result', compact('resource'));
			$config = compact('result', 'query') + ['class' => 'set', 'defaults' => false];
			return $model::create([], $config);
		});
	}