PHP Class 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(); ~~~
Since: 2.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Component, implements yii\db\QueryInterface, use trait yii\db\QueryTrait
Afficher le fichier Open project: yiisoft/yii2-mongodb Class Usage Examples

Méthodes publiques

Свойство Type Description
$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.

Méthodes publiques

Méthode Description
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.

Méthodes protégées

Méthode Description
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

Private Methods

Méthode Description
composeCondition ( ) : array Composes condition from raw [[where]] value.

Method Details

addOptions() public méthode

Adds additional cursor options.
See also: options()
public addOptions ( array $options )
$options array cursor options in format: optionName => optionValue

aggregate() protected méthode

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.
Résultat integer aggregation result.

all() public méthode

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.
Résultat array the query results. If the query results in nothing, an empty array will be returned.

andFilterCompare() public méthode

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.
See also: yii\mongodb\Collection::buildCondition()
Since: 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() public méthode

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.
Résultat integer the average of the specified column values.

batch() public méthode

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 }
Since: 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.
Résultat BatchQueryResult the batch query result. It implements the `Iterator` interface and can be traversed to retrieve the data in batches.

buildCursor() public méthode

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.
Résultat MongoDB\Driver\Cursor mongo cursor instance.

column() public méthode

Column _id will be automatically excluded from select fields, if [[select]] is not empty and _id is not selected explicitly.
Since: 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.
Résultat array the first column of the query result. An empty array is returned if the query results in nothing.

count() public méthode

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.
Résultat integer number of records

distinct() public méthode

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.
Résultat array array of distinct values

each() public méthode

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) { }
Since: 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.
Résultat BatchQueryResult the batch query result. It implements the `Iterator` interface and can be traversed to retrieve the data in batches.

exists() public méthode

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.
Résultat boolean whether the query result contains any row of data.

fetchRows() protected méthode

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.
Résultat array | boolean result.

fetchRowsInternal() protected méthode

See also: 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.
Résultat array | boolean result.

from() public méthode

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

getCollection() public méthode

Returns the Mongo collection for this query.
public getCollection ( Connection $db = null ) : Collection
$db Connection Mongo connection.
Résultat Collection collection instance.

max() public méthode

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.
Résultat integer the maximum of the specified column values.

min() public méthode

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.
Résultat integer the minimum of the specified column values.

modify() public méthode

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.
Résultat array | null the original document, or the modified document when $options['new'] is set.

one() public méthode

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.
Résultat array | boolean the first row (in terms of an array) of the query result. False is returned if the query results in nothing.

options() public méthode

Sets the cursor options.
See also: addOptions()
public options ( array $options )
$options array cursor options in format: optionName => optionValue

populate() public méthode

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
Résultat array the converted query result

scalar() public méthode

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.
Since: 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.
Résultat 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() public méthode

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

sum() public méthode

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.
Résultat integer the sum of the specified column values

Property Details

$from public_oe property

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
See also: from()
public $from

$options public_oe property

cursor options in format: optionKey => optionValue
See also: MongoDB\Driver\Cursor::addOption()
See also: options()
public $options

$select public_oe property

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.
See also: select()
public $select