PHP Class yii\mongodb\QueryBuilder

It is used by Command for particular commands and queries composition. MongoDB uses JSON format to specify query conditions with quite specific syntax. However QueryBuilder::buildCondition method provides the ability of "translating" common condition format used "yii\db\*" into MongoDB condition. For example: php $condition = [ [ 'OR', ['AND', ['first_name' => 'John'], ['last_name' => 'Smith']], ['status' => [1, 2, 3]] ], ]; print_r(Yii::$app->mongodb->getQueryBuilder()->buildCondition($condition)); outputs : [ '$or' => [ [ 'first_name' => 'John', 'last_name' => 'John', ], [ 'status' => ['$in' => [1, 2, 3]], ] ] ] Note: condition values for the key '_id' will be automatically cast to [[\MongoDB\BSON\ObjectID]] instance, even if they are plain strings. However, if you have other columns, containing [[\MongoDB\BSON\ObjectID]], you should take care of possible typecast on your own.
Since: 2.1
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Object
Datei anzeigen Open project: yiisoft/yii2-mongodb

Public Properties

Property Type Description
$db the MongoDB connection.

Public Methods

Method Description
__construct ( Connection $connection, array $config = [] ) Constructor.
aggregate ( string $collectionName, array $pipelines, array $options = [] ) : array Generates 'aggregate' command.
buildAndCondition ( string $operator, array $operands ) : array Connects two or more conditions with the AND operator.
buildBetweenCondition ( string $operator, array $operands ) : array Creates an Mongo condition, which emulates the BETWEEN operator.
buildCondition ( array $condition ) : array Parses the condition specification and generates the corresponding Mongo condition.
buildHashCondition ( array $condition ) : array Creates a condition based on column-value pairs.
buildInCondition ( string $operator, array $operands ) : array Creates an Mongo condition with the IN operator.
buildLikeCondition ( string $operator, array $operands ) : array Creates a Mongo condition, which emulates the LIKE operator.
buildNotCondition ( string $operator, array $operands ) : array Composes NOT condition.
buildOrCondition ( string $operator, array $operands ) : array Connects two or more conditions with the OR operator.
buildRegexCondition ( string $operator, array $operands ) : array Creates a Mongo regular expression condition.
buildSelectFields ( array | string $fields ) : array Normalizes fields list for the MongoDB select composition.
buildSimpleCondition ( string $operator, array $operands ) : string Creates an Mongo condition like {$operator:{field:value}}.
buildSortFields ( array | string $fields ) : array Normalizes fields list for the MongoDB sort composition.
count ( string $collectionName, array $condition = [], array $options = [] ) : array Generates count command
createCollection ( string $collectionName, array $options = [] ) : array Generates 'create collection' command.
createIndexes ( string | null $databaseName, string $collectionName, array[] $indexes ) : array Generates create indexes command.
distinct ( string $collectionName, string $fieldName, array $condition = [], array $options = [] ) : array Generates 'distinct' command.
dropCollection ( string $collectionName ) : array Generates drop collection command.
dropDatabase ( ) : array Generates drop database command.
dropIndexes ( string $collectionName, string $index ) : array Generates drop indexes command.
explain ( string $collectionName, array $query ) : array Generates 'explain' command.
findAndModify ( string $collectionName, array $condition = [], array $update = [], array $options = [] ) : array Generates 'find and modify' command.
group ( string $collectionName, $keys, array $initial, MongoDB\BSON\Javascript | string $reduce, array $options = [] ) : array Generates 'group' command.
listCollections ( array $condition = [], array $options = [] ) : array Generates 'listCollections' command.
listDatabases ( array $condition = [], array $options = [] ) : array Generates 'listDatabases' command.
listIndexes ( string $collectionName, array $options = [] ) : array Generates list indexes command.
mapReduce ( string $collectionName, MongoDB\BSON\Javascript | string $map, MongoDB\BSON\Javascript | string $reduce, string | array $out, array $condition = [], array $options = [] ) : array Generates 'map-reduce' command.

Protected Methods

Method Description
ensureMongoId ( mixed $rawId ) : array | MongoDB\BSON\ObjectID Converts given value into [[ObjectID]] instance.
normalizeConditionKeyword ( string $key ) : string Converts "\yii\db\*" quick condition keyword into actual Mongo condition keyword.

Private Methods

Method Description
buildCompositeInCondition ( string $operator, array $columns, array $values ) : array
generateIndexName ( array $columns ) : string Generates index name for the given column orders.

Method Details

__construct() public method

Constructor.
public __construct ( Connection $connection, array $config = [] )
$connection Connection the database connection.
$config array name-value pairs that will be used to initialize the object properties

aggregate() public method

Generates 'aggregate' command.
public aggregate ( string $collectionName, array $pipelines, array $options = [] ) : array
$collectionName string collection name
$pipelines array list of pipeline operators.
$options array optional parameters.
return array command document.

buildAndCondition() public method

Connects two or more conditions with the AND operator.
public buildAndCondition ( string $operator, array $operands ) : array
$operator string the operator to use for connecting the given operands
$operands array the Mongo conditions to connect.
return array the generated Mongo condition.

buildBetweenCondition() public method

Creates an Mongo condition, which emulates the BETWEEN operator.
public buildBetweenCondition ( string $operator, array $operands ) : array
$operator string the operator to use
$operands array the first operand is the column name. The second and third operands describe the interval that column value should be in.
return array the generated Mongo condition.

buildCondition() public method

Parses the condition specification and generates the corresponding Mongo condition.
public buildCondition ( array $condition ) : array
$condition array the condition specification. Please refer to [[Query::where()]] on how to specify a condition.
return array the generated Mongo condition

buildHashCondition() public method

Creates a condition based on column-value pairs.
public buildHashCondition ( array $condition ) : array
$condition array the condition specification.
return array the generated Mongo condition.

buildInCondition() public method

Creates an Mongo condition with the IN operator.
public buildInCondition ( string $operator, array $operands ) : array
$operator string the operator to use (e.g. `IN` or `NOT IN`)
$operands array the first operand is the column name. If it is an array a composite IN condition will be generated. The second operand is an array of values that column value should be among.
return array the generated Mongo condition.

buildLikeCondition() public method

Creates a Mongo condition, which emulates the LIKE operator.
public buildLikeCondition ( string $operator, array $operands ) : array
$operator string the operator to use
$operands array the first operand is the column name. The second operand is a single value that column value should be compared with.
return array the generated Mongo condition.

buildNotCondition() public method

Composes NOT condition.
public buildNotCondition ( string $operator, array $operands ) : array
$operator string the operator to use for connecting the given operands
$operands array the Mongo conditions to connect.
return array the generated Mongo condition.

buildOrCondition() public method

Connects two or more conditions with the OR operator.
public buildOrCondition ( string $operator, array $operands ) : array
$operator string the operator to use for connecting the given operands
$operands array the Mongo conditions to connect.
return array the generated Mongo condition.

buildRegexCondition() public method

Creates a Mongo regular expression condition.
public buildRegexCondition ( string $operator, array $operands ) : array
$operator string the operator to use
$operands array the first operand is the column name. The second operand is a single value that column value should be compared with.
return array the generated Mongo condition.

buildSelectFields() public method

Normalizes fields list for the MongoDB select composition.
public buildSelectFields ( array | string $fields ) : array
$fields array | string raw fields.
return array normalized select fields.

buildSimpleCondition() public method

Creates an Mongo condition like {$operator:{field:value}}.
public buildSimpleCondition ( string $operator, array $operands ) : string
$operator string the operator to use. Besides regular MongoDB operators, aliases like `>`, `<=`, and so on, can be used here.
$operands array the first operand is the column name. The second operand is a single value that column value should be compared with.
return string the generated Mongo condition.

buildSortFields() public method

Normalizes fields list for the MongoDB sort composition.
public buildSortFields ( array | string $fields ) : array
$fields array | string raw fields.
return array normalized sort fields.

count() public method

Generates count command
public count ( string $collectionName, array $condition = [], array $options = [] ) : array
$collectionName string
$condition array
$options array
return array command document.

createCollection() public method

https://docs.mongodb.com/manual/reference/method/db.createCollection/
public createCollection ( string $collectionName, array $options = [] ) : array
$collectionName string collection name.
$options array collection options in format: "name" => "value"
return array command document.

createIndexes() public method

Generates create indexes command.
See also: https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/
public createIndexes ( string | null $databaseName, string $collectionName, array[] $indexes ) : array
$databaseName string | null database name.
$collectionName string collection name.
$indexes array[] indexes specification. Each specification should be an array in format: optionName => value The main options are: - keys: array, column names with sort order, to be indexed. This option is mandatory. - unique: bool, whether to create unique index. - name: string, the name of the index, if not set it will be generated automatically. - background: bool, whether to bind index in the background. - sparse: bool, whether index should reference only documents with the specified field. See [[https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#options-for-all-index-types]] for the full list of options.
return array command document.

distinct() public method

Generates 'distinct' command.
public distinct ( string $collectionName, string $fieldName, array $condition = [], array $options = [] ) : array
$collectionName string collection name.
$fieldName string target field name.
$condition array filter condition
$options array list of options in format: optionName => optionValue.
return array command document.

dropCollection() public method

https://docs.mongodb.com/manual/reference/method/db.collection.drop/
public dropCollection ( string $collectionName ) : array
$collectionName string name of the collection to be dropped.
return array command document.

dropDatabase() public method

https://docs.mongodb.com/manual/reference/method/db.dropDatabase/
public dropDatabase ( ) : array
return array command document.

dropIndexes() public method

Generates drop indexes command.
public dropIndexes ( string $collectionName, string $index ) : array
$collectionName string collection name
$index string index name or pattern, use `*` in order to drop all indexes.
return array command document.

ensureMongoId() protected method

If array given, each element of it will be processed.
protected ensureMongoId ( mixed $rawId ) : array | MongoDB\BSON\ObjectID
$rawId mixed raw id(s).
return array | MongoDB\BSON\ObjectID normalized id(s).

explain() public method

Generates 'explain' command.
public explain ( string $collectionName, array $query ) : array
$collectionName string collection name.
$query array query options.
return array command document.

findAndModify() public method

Generates 'find and modify' command.
public findAndModify ( string $collectionName, array $condition = [], array $update = [], array $options = [] ) : array
$collectionName string collection name
$condition array filter condition
$update array update criteria
$options array list of options in format: optionName => optionValue.
return array command document.

group() public method

Generates 'group' command.
public group ( string $collectionName, $keys, array $initial, MongoDB\BSON\Javascript | string $reduce, array $options = [] ) : array
$collectionName string
$initial array Initial value of the aggregation counter object.
$reduce MongoDB\BSON\Javascript | string function that takes two arguments (the current document and the aggregation to this point) and does the aggregation. Argument will be automatically cast to [[Javascript]].
$options array optional parameters to the group command. Valid options include: - condition - criteria for including a document in the aggregation. - finalize - function called once per unique key that takes the final output of the reduce function.
return array command document.

listCollections() public method

Generates 'listCollections' command.
public listCollections ( array $condition = [], array $options = [] ) : array
$condition array filter condition.
$options array command options.
return array command document.

listDatabases() public method

Generates 'listDatabases' command.
public listDatabases ( array $condition = [], array $options = [] ) : array
$condition array filter condition.
$options array command options.
return array command document.

listIndexes() public method

Generates list indexes command.
public listIndexes ( string $collectionName, array $options = [] ) : array
$collectionName string collection name
$options array command options. Available options are: - maxTimeMS: int, max execution time in ms.
return array command document.

mapReduce() public method

Generates 'map-reduce' command.
See also: https://docs.mongodb.com/manual/core/map-reduce/
public mapReduce ( string $collectionName, MongoDB\BSON\Javascript | string $map, MongoDB\BSON\Javascript | string $reduce, string | array $out, array $condition = [], array $options = [] ) : array
$collectionName string collection name.
$map MongoDB\BSON\Javascript | string function, which emits map data from collection. Argument will be automatically cast to [[\MongoDB\BSON\Javascript]].
$reduce MongoDB\BSON\Javascript | string function that takes two arguments (the map key and the map values) and does the aggregation. Argument will be automatically cast to [[\MongoDB\BSON\Javascript]].
$out string | array output collection name. It could be a string for simple output ('outputCollection'), or an array for parametrized output (['merge' => 'outputCollection']). You can pass ['inline' => true] to fetch the result at once without temporary collection usage.
$condition array filter condition for including a document in the aggregation.
$options array additional optional parameters to the mapReduce command. Valid options include: - sort: array, key to sort the input documents. The sort key must be in an existing index for this collection. - limit: int, the maximum number of documents to return in the collection. - finalize: \MongoDB\BSON\Javascript|string, function, which follows the reduce method and modifies the output. - scope: array, specifies global variables that are accessible in the map, reduce and finalize functions. - jsMode: bool, specifies whether to convert intermediate data into BSON format between the execution of the map and reduce functions. - verbose: bool, specifies whether to include the timing information in the result information.
return array command document.

normalizeConditionKeyword() protected method

Converts "\yii\db\*" quick condition keyword into actual Mongo condition keyword.
protected normalizeConditionKeyword ( string $key ) : string
$key string raw condition key.
return string actual key.

Property Details

$db public_oe property

the MongoDB connection.
public $db