Execute the find() method to select records and return a cursor object.
If a group by is declared, execute the group() method instead.

@param \MongoCollection $collection
@param \Titon\Db\Query $query
@return \MongoCursor|array
173840
public function executeSelect(MongoCollection $collection, Query $query) {
        $where = $this->formatWhere($query->getWhere());

        if ($groupBy = $query->getGroupBy()) {
            $groupBy = $this->formatGroupBy($groupBy);

            $response = $collection->group($groupBy, ['items' => []], 'function(item, results) { results.items.push(item); }', [
                'condition' => $where
            ]);

            $response['params'] = [
                'where' => $where,
                'groupBy' => $groupBy
            ];

            return $response;
        }

        // Regular find using a cursor
        $cursor = $collection->find($where, $this->formatFields($query));

        if ($orderBy = $query->getOrderBy()) {
            $cursor->sort($this->formatOrderBy($orderBy));
        }

        if ($offset = $query->getOffset()) {
            $cursor->skip($this->formatLimitOffset(null, $offset));
        }

        if ($limit = $query->getLimit()) {
            $cursor->limit($this->formatLimit($limit));
        }

        return $cursor;
    }
1172785
public static function query($query)
    {
        //
        static::applySchema();

        //
        $table = self::getTable();
        $whereArray = [];

        //
        if (isset($query['where'])) {
            $whereArray[] = '('.$query['where'].')';
            unset($query['where']);
        }

        //
        foreach ($query as $field => $value) {
            if (in_array($field, ['order', 'limit'])) {
                continue;
            }
            $whereArray[] = "{$field} = '{$value}'";
        }

        //
        $where = count($whereArray) > 0 ? 'WHERE '.implode(' AND ', $whereArray) : '';
        $order = isset($query['order']) ? 'ORDER BY '.$query['order'] : '';
        $limit = isset($query['limit']) ? 'LIMIT '.$query['limit'] : '';
        $sql = "SELECT * FROM {$table} {$where} {$order} {$limit}";
        $results = static::getDatabase()->getResults($sql);

        //
        foreach ($results as &$record) {
            $record = static::create($record);
        }

        return $results;
    }
23785
public function apply(Query $query) : Query
    {
        return $query->where(
            (new QueryExpression())->gt(
                $this->field,
                $this->value,
                $query->getTypeMap()->type($this->field)
            )
        );
    }
1289780
public function search($query)
    {
        $criteria = [];
        foreach ($query as $term) {
            $criteria[$term->name] = $term->toparam();
        }
        $pager = [
            'object' => $this,
            'method' => 'fetchDisputes',
            'query' => $criteria
        ];
        return new PaginatedCollection($pager);
    }
216379
public function getOne($collection = "")
		{
			if (empty($collection)) {
				throw new Exception("In order to retrieve documents from MongoDB you must provide a collection name.");
			}

			$returns = $this->db->{$collection}->findOne($this->wheres, $this->selects);

			$this->_clear();

			return $returns;
		}
539562
public static function count($collection, $query = null)
  {
    static::checkConnexion();
    $collection = self::$db->$collection;
    if (is_string($query)) {
      $query = json_decode($query, true);
      if(!$query){
        throw new \Exception('Requête JSON formulée incorrectement');
      }
    }
    if ($query) {
      return $collection->count($query);
    } else {
      return $collection->count();
    }
  }
183773
public function getCursor(Query $query)
    {
        // 分析查询表达式
        $options = $query->getOptions();

        // 生成MongoQuery对象
        $mongoQuery = $this->builder->select($query);

        // 执行查询操作
        $readPreference = isset($options['readPreference']) ? $options['readPreference'] : null;

        return $this->query($options['table'], $mongoQuery, $readPreference, true, $options['typeMap']);
    }
467885
private function getCommand($query): MongoCommand
    {
        try {
            $command = new MongoCommand($query);
        } catch (InvalidArgumentException $e) {
            new Exception('WriteConcern could not to be initiated');
        }
        return $command;
    }
933500
public function fetch($query, $values = null)
  {
    $m  = $this->getManager();
    $st = $m->query($query, $values);

    /** @var $collection Collection */
    $class = $this->getCollectionClass();
    $collection = new $class;
    while ($obj = $this->fetchObject($st)) {
      if ($pk = $obj->getPrimaryKey()) {
        $collection->set($obj->get($pk), $obj);
      } else {
        $collection->add($obj);
      }
    }

    return $collection;
  }
792136
public function qualifyCollectionQuery($query)
    {
        $user = auth()->user();

        $modelPolicy = Gate::getPolicyFor(static::$model);

        // If no policy exists for this model, then there's nothing to check
        if (is_null($modelPolicy)) {
            return $query;
        }

        // Add conditions to the query, if they are defined in the model's policy
        if (method_exists($modelPolicy, 'qualifyCollectionQueryWithUser')) {
            $query = $modelPolicy->qualifyCollectionQueryWithUser($user, $query);
        }

        return $query;
    }
639074
public function hasKey($key, $collection)
    {
        if (empty($this->queriesResultCache[ 'collectionKeys' ][ $collection ])) {
            $this->getKeys($collection);
        }

        return (bool)in_array($key, $this->queriesResultCache[ 'collectionKeys' ][ $collection ]);
    }
935180
public function delete($collection = '')
	{
		if (empty($collection))
		{
			throw new \Mongo_DbException("No Mongo collection selected to delete from");
		}

		try
		{
			if ($this->profiling)
			{
				$query = json_encode(array(
				'type'			=> 'delete',
				'collection'	=> $collection,
				'where'			=> $this->wheres,
				));

				$benchmark = \Profiler::start((string) $this->db, $query);
			}

			$this->db->{$collection}->remove($this->wheres, array('fsync' => true, 'justOne' => true));

			if (isset($benchmark))
			{
				\Profiler::stop($benchmark);
			}

			$this->_clear();
			return true;
		}
		catch (\MongoCursorException $e)
		{
			throw new \Mongo_DbException("Delete of data into MongoDB failed: {$e->getMessage()}", $e->getCode());
		}
	}
750742
public function cursor(Query $query)
    {
        // 分析查询表达式
        $options = $query->getOptions();

        // 生成查询SQL
        $sql = $this->builder->select($query);

        $bind = $query->getBind();

        $condition = isset($options['where']['AND']) ? $options['where']['AND'] : null;
        $relation  = isset($options['relaltion']) ? $options['relation'] : null;

        // 执行查询操作
        return $this->getCursor($sql, $bind, $options['master'], $query->getModel(), $condition, $relation);
    }
649148
public static function aggregate($query)
    {
        $rows = self::connection()->aggregate(self::collectionName(), $query);
        return $rows;
    }
977969
protected function doFind(array $query, array $fields)
    {
        $mongoCollection = $this->mongoCollection;
        $cursor = $this->retry(function() use ($mongoCollection, $query, $fields) {
            return $mongoCollection->find($query, $fields);
        });
        return $this->wrapCursor($cursor, $query, $fields);
    }
884413
private function visitQuery(Collection $collection, VisitorInterface $subVisitor): ?array
    {
        if (!$collection->hasQuery()) {
            return null;
        }

        return $subVisitor->visit($collection->getQuery());
    }
1058133
public function selectCollection($db, $collection)
    {
        $this->initialize();
        return $this->selectDatabase($db)->selectCollection($collection);
    }