PHP Класс yii\mongodb\Query

Query provides a set of methods to facilitate the specification of "find" command. These methods can be chained together. For example, ~~~ $query = new Query; compose the query $query->select(['name', 'status']) ->from('customer') ->limit(10); execute the query $rows = $query->all(); ~~~
С версии: 2.0
Автор: Paul Klimov ([email protected])
Наследование: extends yii\base\Component, implements yii\db\QueryInterface, use trait yii\db\QueryTrait
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$from the collection to be selected from. If string considered as the name of the collection inside the default database. If array - first element considered as the name of the database, second - as name of collection inside that database
$options cursor options in format: optionKey => optionValue
$select the fields of the results to return. For example: ['name', 'group_id'], ['name' => true, '_id' => false]. Unless directly excluded, the "_id" field is always returned. If not set, it means selecting all columns.

Открытые методы

Метод Описание
addOptions ( array $options ) Adds additional cursor options.
all ( Connection $db = null ) : array Executes the query and returns all results as an array.
andFilterCompare ( string $name, string $value, string $defaultOperator = '=' ) Helper method for easy querying on values containing some common operators.
average ( string $q, Connection $db = null ) : integer Returns the average of the specified column values.
batch ( integer $batchSize = 100, Connection $db = null ) : BatchQueryResult Starts a batch query.
buildCursor ( Connection $db = null ) : MongoDB\Driver\Cursor Builds the Mongo cursor for this query.
column ( Connection $db = null ) : array Executes the query and returns the first column of the result.
count ( string $q = '*', Connection $db = null ) : integer Returns the number of records.
distinct ( string $q, Connection $db = null ) : array Returns a list of distinct values for the given column across a collection.
each ( integer $batchSize = 100, Connection $db = null ) : BatchQueryResult Starts a batch query and retrieves data row by row.
exists ( Connection $db = null ) : boolean Returns a value indicating whether the query result contains any row of data.
from ( $collection ) Sets the collection to be selected from.
getCollection ( Connection $db = null ) : Collection Returns the Mongo collection for this query.
max ( string $q, Connection $db = null ) : integer Returns the maximum of the specified column values.
min ( string $q, Connection $db = null ) : integer Returns the minimum of the specified column values.
modify ( array $update, array $options = [], Connection $db = null ) : array | null Performs 'findAndModify' query and returns a single row of result.
one ( Connection $db = null ) : array | boolean Executes the query and returns a single row of result.
options ( array $options ) Sets the cursor options.
populate ( array $rows ) : array Converts the raw query results into the format as specified by this query.
scalar ( Connection $db = null ) : string | null | false Returns the query result as a scalar value.
select ( array $fields ) Sets the list of fields of the results to return.
sum ( string $q, Connection $db = null ) : integer Returns the sum of the specified column values.

Защищенные методы

Метод Описание
aggregate ( string $column, string $operator, Connection $db ) : integer Performs the aggregation for the given column.
fetchRows ( MongoDB\Driver\Cursor $cursor, boolean $all = true, string | callable $indexBy = null ) : array | boolean Fetches rows from the given Mongo cursor.
fetchRowsInternal ( MongoDB\Driver\Cursor $cursor, boolean $all ) : array | boolean

Приватные методы

Метод Описание
composeCondition ( ) : array Composes condition from raw [[where]] value.

Описание методов

addOptions() публичный Метод

Adds additional cursor options.
См. также: options()
public addOptions ( array $options )
$options array cursor options in format: optionName => optionValue

aggregate() защищенный Метод

Performs the aggregation for the given column.
protected aggregate ( string $column, string $operator, Connection $db ) : integer
$column string column name.
$operator string aggregation operator.
$db Connection the database connection used to execute the query.
Результат integer aggregation result.

all() публичный Метод

Executes the query and returns all results as an array.
public all ( Connection $db = null ) : array
$db Connection the Mongo connection used to execute the query. If this parameter is not given, the `mongodb` application component will be used.
Результат array the query results. If the query results in nothing, an empty array will be returned.

andFilterCompare() публичный Метод

The comparison operator is intelligently determined based on the first few characters in the given value and internally translated to a MongoDB operator. In particular, it recognizes the following operators if they appear as the leading characters in the given value: <: the column must be less than the given value ($lt). >: the column must be greater than the given value ($gt). <=: the column must be less than or equal to the given value ($lte). >=: the column must be greater than or equal to the given value ($gte). <>: the column must not be the same as the given value ($ne). Note that when $partialMatch is true, this would mean the value must not be a substring of the column. =: the column must be equal to the given value ($eq). none of the above: use the $defaultOperator Note that when the value is empty, no comparison expression will be added to the search condition.
См. также: yii\mongodb\Collection::buildCondition()
С версии: 2.0.5
public andFilterCompare ( string $name, string $value, string $defaultOperator = '=' )
$name string column name
$value string column value
$defaultOperator string Defaults to =, performing an exact match. For example: use 'LIKE' or 'REGEX' for partial cq regex matching

average() публичный Метод

Returns the average of the specified column values.
public average ( string $q, Connection $db = null ) : integer
$q string the column name. Make sure you properly quote column names in the expression.
$db Connection the Mongo connection used to execute the query. If this parameter is not given, the `mongodb` application component will be used.
Результат integer the average of the specified column values.

batch() публичный Метод

A batch query supports fetching data in batches, which can keep the memory usage under a limit. This method will return a BatchQueryResult object which implements the Iterator interface and can be traversed to retrieve the data in batches. For example, php $query = (new Query)->from('user'); foreach ($query->batch() as $rows) { $rows is an array of 10 or fewer rows from user collection }
С версии: 2.1
public batch ( integer $batchSize = 100, Connection $db = null ) : BatchQueryResult
$batchSize integer the number of records to be fetched in each batch.
$db Connection the MongoDB connection. If not set, the "mongodb" application component will be used.
Результат BatchQueryResult the batch query result. It implements the `Iterator` interface and can be traversed to retrieve the data in batches.

buildCursor() публичный Метод

Builds the Mongo cursor for this query.
public buildCursor ( Connection $db = null ) : MongoDB\Driver\Cursor
$db Connection the database connection used to execute the query.
Результат MongoDB\Driver\Cursor mongo cursor instance.

column() публичный Метод

Column _id will be automatically excluded from select fields, if [[select]] is not empty and _id is not selected explicitly.
С версии: 2.1.2
public column ( Connection $db = null ) : array
$db Connection the MongoDB connection used to generate the query. If this parameter is not given, the `mongodb` application component will be used.
Результат array the first column of the query result. An empty array is returned if the query results in nothing.

count() публичный Метод

Returns the number of records.
public count ( string $q = '*', Connection $db = null ) : integer
$q string kept to match [[QueryInterface]], its value is ignored.
$db Connection the Mongo connection used to execute the query. If this parameter is not given, the `mongodb` application component will be used.
Результат integer number of records

distinct() публичный Метод

Returns a list of distinct values for the given column across a collection.
public distinct ( string $q, Connection $db = null ) : array
$q string column to use.
$db Connection the Mongo connection used to execute the query. If this parameter is not given, the `mongodb` application component will be used.
Результат array array of distinct values

each() публичный Метод

This method is similar to Query::batch except that in each iteration of the result, only one row of data is returned. For example, php $query = (new Query)->from('user'); foreach ($query->each() as $row) { }
С версии: 2.1
public each ( integer $batchSize = 100, Connection $db = null ) : BatchQueryResult
$batchSize integer the number of records to be fetched in each batch.
$db Connection the MongoDB connection. If not set, the "mongodb" application component will be used.
Результат BatchQueryResult the batch query result. It implements the `Iterator` interface and can be traversed to retrieve the data in batches.

exists() публичный Метод

Returns a value indicating whether the query result contains any row of data.
public exists ( Connection $db = null ) : boolean
$db Connection the Mongo connection used to execute the query. If this parameter is not given, the `mongodb` application component will be used.
Результат boolean whether the query result contains any row of data.

fetchRows() защищенный Метод

Fetches rows from the given Mongo cursor.
protected fetchRows ( MongoDB\Driver\Cursor $cursor, boolean $all = true, string | callable $indexBy = null ) : array | boolean
$cursor MongoDB\Driver\Cursor Mongo cursor instance to fetch data from.
$all boolean whether to fetch all rows or only first one.
$indexBy string | callable the column name or PHP callback, by which the query results should be indexed by.
Результат array | boolean result.

fetchRowsInternal() защищенный Метод

См. также: Query::fetchRows()
protected fetchRowsInternal ( MongoDB\Driver\Cursor $cursor, boolean $all ) : array | boolean
$cursor MongoDB\Driver\Cursor Mongo cursor instance to fetch data from.
$all boolean whether to fetch all rows or only first one.
Результат array | boolean result.

from() публичный Метод

Sets the collection to be selected from.
public from ( $collection )

getCollection() публичный Метод

Returns the Mongo collection for this query.
public getCollection ( Connection $db = null ) : Collection
$db Connection Mongo connection.
Результат Collection collection instance.

max() публичный Метод

Returns the maximum of the specified column values.
public max ( string $q, Connection $db = null ) : integer
$q string the column name. Make sure you properly quote column names in the expression.
$db Connection the Mongo connection used to execute the query. If this parameter is not given, the `mongodb` application component will be used.
Результат integer the maximum of the specified column values.

min() публичный Метод

Returns the minimum of the specified column values.
public min ( string $q, Connection $db = null ) : integer
$q string the column name. Make sure you properly quote column names in the expression.
$db Connection the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used.
Результат integer the minimum of the specified column values.

modify() публичный Метод

Performs 'findAndModify' query and returns a single row of result.
public modify ( array $update, array $options = [], Connection $db = null ) : array | null
$update array update criteria
$options array list of options in format: optionName => optionValue.
$db Connection the Mongo connection used to execute the query.
Результат array | null the original document, or the modified document when $options['new'] is set.

one() публичный Метод

Executes the query and returns a single row of result.
public one ( Connection $db = null ) : array | boolean
$db Connection the Mongo connection used to execute the query. If this parameter is not given, the `mongodb` application component will be used.
Результат array | boolean the first row (in terms of an array) of the query result. False is returned if the query results in nothing.

options() публичный Метод

Sets the cursor options.
См. также: addOptions()
public options ( array $options )
$options array cursor options in format: optionName => optionValue

populate() публичный Метод

This method is internally used to convert the data fetched from database into the format as required by this query.
public populate ( array $rows ) : array
$rows array the raw query result from database
Результат array the converted query result

scalar() публичный Метод

The value returned will be the first column in the first row of the query results. Column _id will be automatically excluded from select fields, if [[select]] is not empty and _id is not selected explicitly.
С версии: 2.1.2
public scalar ( Connection $db = null ) : string | null | false
$db Connection the MongoDB connection used to generate the query. If this parameter is not given, the `mongodb` application component will be used.
Результат string | null | false the value of the first column in the first row of the query result. `false` is returned if the query result is empty.

select() публичный Метод

Sets the list of fields of the results to return.
public select ( array $fields )
$fields array fields of the results to return.

sum() публичный Метод

Returns the sum of the specified column values.
public sum ( string $q, Connection $db = null ) : integer
$q string the column name. Make sure you properly quote column names in the expression.
$db Connection the Mongo connection used to execute the query. If this parameter is not given, the `mongodb` application component will be used.
Результат integer the sum of the specified column values

Описание свойств

$from публичное свойство

the collection to be selected from. If string considered as the name of the collection inside the default database. If array - first element considered as the name of the database, second - as name of collection inside that database
См. также: from()
public $from

$options публичное свойство

cursor options in format: optionKey => optionValue
См. также: MongoDB\Driver\Cursor::addOption()
См. также: options()
public $options

$select публичное свойство

the fields of the results to return. For example: ['name', 'group_id'], ['name' => true, '_id' => false]. Unless directly excluded, the "_id" field is always returned. If not set, it means selecting all columns.
См. также: select()
public $select