Class yii\mongodb\Query

Inheritanceyii\mongodb\Query » yii\base\Component » yii\base\Object
Implementsyii\base\Configurable, yii\db\QueryInterface
Uses Traitsyii\db\QueryTrait
Subclassesyii\mongodb\ActiveQuery, yii\mongodb\file\ActiveQuery, yii\mongodb\file\Query
Available since version2.0
Source Code https://github.com/yiisoft/yii2-mongodb/blob/master/Query.php

Query represents Mongo "find" operation.

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();

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$collection yii\mongodb\Collection Collection instance. yii\mongodb\Query
$from string|array The collection to be selected from. yii\mongodb\Query
$indexBy string|callable The name of the column by which the query results should be indexed by. yii\db\QueryTrait
$limit integer Maximum number of records to be returned. yii\db\QueryTrait
$offset integer Zero-based offset from where the records are to be returned. yii\db\QueryTrait
$orderBy array How to sort the query results. yii\db\QueryTrait
$select array The fields of the results to return. yii\mongodb\Query
$where string|array Query condition. yii\db\QueryTrait

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Object
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addOrderBy() Adds additional ORDER BY columns to the query. yii\db\QueryTrait
all() Executes the query and returns all results as an array. yii\mongodb\Query
andFilterWhere() Adds an additional WHERE condition to the existing one but ignores empty operands. yii\db\QueryTrait
andWhere() Adds an additional WHERE condition to the existing one. yii\db\QueryTrait
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
average() Returns the average of the specified column values. yii\mongodb\Query
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\Object
count() Returns the number of records. yii\mongodb\Query
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
distinct() Returns a list of distinct values for the given column across a collection. yii\mongodb\Query
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
exists() Returns a value indicating whether the query result contains any row of data. yii\mongodb\Query
filterWhere() Sets the WHERE part of the query but ignores empty operands. yii\db\QueryTrait
from() Sets the collection to be selected from. yii\mongodb\Query
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getCollection() Returns the Mongo collection for this query. yii\mongodb\Query
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
indexBy() Sets the indexBy() property. yii\db\QueryTrait
init() Initializes the object. yii\base\Object
limit() Sets the LIMIT part of the query. yii\db\QueryTrait
max() Returns the maximum of the specified column values. yii\mongodb\Query
min() Returns the minimum of the specified column values. yii\mongodb\Query
modify() Performs 'findAndModify' query and returns a single row of result. yii\mongodb\Query
off() Detaches an existing event handler from this component. yii\base\Component
offset() Sets the OFFSET part of the query. yii\db\QueryTrait
on() Attaches an event handler to an event. yii\base\Component
one() Executes the query and returns a single row of result. yii\mongodb\Query
orFilterWhere() Adds an additional WHERE condition to the existing one but ignores empty operands. yii\db\QueryTrait
orWhere() Adds an additional WHERE condition to the existing one. yii\db\QueryTrait
orderBy() Sets the ORDER BY part of the query. yii\db\QueryTrait
populate() Converts the raw query results into the format as specified by this query. yii\mongodb\Query
select() Sets the list of fields of the results to return. yii\mongodb\Query
sum() Returns the sum of the specified column values. yii\mongodb\Query
trigger() Triggers an event. yii\base\Component
where() Sets the WHERE part of the query. yii\db\QueryTrait

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
aggregate() Performs the aggregation for the given column. yii\mongodb\Query
buildCursor() Builds the Mongo cursor for this query. yii\mongodb\Query
fetchRows() Fetches rows from the given Mongo cursor. yii\mongodb\Query
fetchRowsInternal() yii\mongodb\Query
filterCondition() Removes empty operands from the given query condition. yii\db\QueryTrait
isEmpty() Returns a value indicating whether the give value is "empty". yii\db\QueryTrait
normalizeOrderBy() Normalizes format of ORDER BY data yii\db\QueryTrait

Property Details

$collection public read-only property

Collection instance.

$from public 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().

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

Method Details

aggregate() protected method

Performs the aggregation for the given column.

integer aggregate$column$operator$db )
$column string

Column name.

$operator string

Aggregation operator.

$db yii\mongodb\Connection

The database connection used to execute the query.

return integer

Aggregation result.

all() public method

Executes the query and returns all results as an array.

array all$db null )
$db yii\mongodb\Connection

The Mongo connection used to execute the query. If this parameter is not given, the mongodb application component will be used.

return array

The query results. If the query results in nothing, an empty array will be returned.

average() public method

Returns the average of the specified column values.

integer average$q$db null )
$q string

The column name. Make sure you properly quote column names in the expression.

$db yii\mongodb\Connection

The Mongo connection used to execute the query. If this parameter is not given, the mongodb application component will be used.

return integer

The average of the specified column values.

buildCursor() protected method

Builds the Mongo cursor for this query.

\MongoCursor buildCursor$db null )
$db yii\mongodb\Connection

The database connection used to execute the query.

return \MongoCursor

Mongo cursor instance.

count() public method

Returns the number of records.

integer count$q '*'$db null )
$q string

Kept to match yii\db\QueryInterface, its value is ignored.

$db yii\mongodb\Connection

The Mongo connection used to execute the query. If this parameter is not given, the mongodb application component will be used.

return integer

Number of records

throws yii\mongodb\Exception

on failure.

distinct() public method

Returns a list of distinct values for the given column across a collection.

array distinct$q$db null )
$q string

Column to use.

$db yii\mongodb\Connection

The Mongo connection used to execute the query. If this parameter is not given, the mongodb application component will be used.

return array

Array of distinct values

exists() public method

Returns a value indicating whether the query result contains any row of data.

boolean exists$db null )
$db yii\mongodb\Connection

The Mongo connection used to execute the query. If this parameter is not given, the mongodb application component will be used.

return boolean

Whether the query result contains any row of data.

fetchRows() protected method

Fetches rows from the given Mongo cursor.

array|boolean fetchRows$cursor$all true$indexBy null )
$cursor \MongoCursor

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.

return array|boolean

Result.

throws yii\mongodb\Exception

on failure.

fetchRowsInternal() protected method
array|boolean fetchRowsInternal$cursor$all$indexBy )
$cursor \MongoCursor

Mongo cursor instance to fetch data from.

$all boolean

Whether to fetch all rows or only first one.

$indexBy string|callable

Value to index by.

return array|boolean

Result.

from() public method

Sets the collection to be selected from.

static from$collection )
$collection
return static

The query object itself.

getCollection() public method

Returns the Mongo collection for this query.

yii\mongodb\Collection getCollection$db null )
$db yii\mongodb\Connection

Mongo connection.

return yii\mongodb\Collection

Collection instance.

max() public method

Returns the maximum of the specified column values.

integer max$q$db null )
$q string

The column name. Make sure you properly quote column names in the expression.

$db yii\mongodb\Connection

The Mongo connection used to execute the query. If this parameter is not given, the mongodb application component will be used.

return integer

The maximum of the specified column values.

min() public method

Returns the minimum of the specified column values.

integer min$q$db null )
$q string

The column name. Make sure you properly quote column names in the expression.

$db yii\mongodb\Connection

The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.

return integer

The minimum of the specified column values.

modify() public method

Performs 'findAndModify' query and returns a single row of result.

array|null modify$update$options = [], $db null )
$update array

Update criteria

$options array

List of options in format: optionName => optionValue.

$db yii\mongodb\Connection

The Mongo connection used to execute the query.

return array|null

The original document, or the modified document when $options['new'] is set.

one() public method

Executes the query and returns a single row of result.

array|boolean one$db null )
$db yii\mongodb\Connection

The Mongo connection used to execute the query. If this parameter is not given, the mongodb application component will be used.

return array|boolean

The first row (in terms of an array) of the query result. False is returned if the query results in nothing.

populate() public method

Converts the raw query results into the format as specified by this query.

This method is internally used to convert the data fetched from database into the format as required by this query.

array populate$rows )
$rows array

The raw query result from database

return array

The converted query result

select() public method

Sets the list of fields of the results to return.

static select( array $fields )
$fields array

Fields of the results to return.

return static

The query object itself.

sum() public method

Returns the sum of the specified column values.

integer sum$q$db null )
$q string

The column name. Make sure you properly quote column names in the expression.

$db yii\mongodb\Connection

The Mongo connection used to execute the query. If this parameter is not given, the mongodb application component will be used.

return integer

The sum of the specified column values