PHP Класс 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.
С версии: 2.1
Автор: Paul Klimov ([email protected])
Наследование: extends yii\base\Object
Показать файл Открыть проект

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

Свойство Тип Описание
$db the MongoDB connection.

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

Метод Описание
__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.

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

Метод Описание
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.

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

Метод Описание
buildCompositeInCondition ( string $operator, array $columns, array $values ) : array
generateIndexName ( array $columns ) : string Generates index name for the given column orders.

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

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

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() публичный Метод

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.
Результат array command document.

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

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.
Результат array the generated Mongo condition.

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

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.
Результат array the generated Mongo condition.

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

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.
Результат array the generated Mongo condition

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

Creates a condition based on column-value pairs.
public buildHashCondition ( array $condition ) : array
$condition array the condition specification.
Результат array the generated Mongo condition.

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

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.
Результат array the generated Mongo condition.

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

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.
Результат array the generated Mongo condition.

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

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.
Результат array the generated Mongo condition.

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

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.
Результат array the generated Mongo condition.

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

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.
Результат array the generated Mongo condition.

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

Normalizes fields list for the MongoDB select composition.
public buildSelectFields ( array | string $fields ) : array
$fields array | string raw fields.
Результат array normalized select fields.

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

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.
Результат string the generated Mongo condition.

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

Normalizes fields list for the MongoDB sort composition.
public buildSortFields ( array | string $fields ) : array
$fields array | string raw fields.
Результат array normalized sort fields.

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

Generates count command
public count ( string $collectionName, array $condition = [], array $options = [] ) : array
$collectionName string
$condition array
$options array
Результат array command document.

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

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"
Результат array command document.

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

Generates create indexes command.
См. также: 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.
Результат array command document.

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

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.
Результат array command document.

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

https://docs.mongodb.com/manual/reference/method/db.collection.drop/
public dropCollection ( string $collectionName ) : array
$collectionName string name of the collection to be dropped.
Результат array command document.

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

https://docs.mongodb.com/manual/reference/method/db.dropDatabase/
public dropDatabase ( ) : array
Результат array command document.

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

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.
Результат array command document.

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

If array given, each element of it will be processed.
protected ensureMongoId ( mixed $rawId ) : array | MongoDB\BSON\ObjectID
$rawId mixed raw id(s).
Результат array | MongoDB\BSON\ObjectID normalized id(s).

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

Generates 'explain' command.
public explain ( string $collectionName, array $query ) : array
$collectionName string collection name.
$query array query options.
Результат array command document.

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

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.
Результат array command document.

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

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.
Результат array command document.

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

Generates 'listCollections' command.
public listCollections ( array $condition = [], array $options = [] ) : array
$condition array filter condition.
$options array command options.
Результат array command document.

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

Generates 'listDatabases' command.
public listDatabases ( array $condition = [], array $options = [] ) : array
$condition array filter condition.
$options array command options.
Результат array command document.

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

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.
Результат array command document.

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

Generates 'map-reduce' command.
См. также: 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.
Результат array command document.

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

Converts "\yii\db\*" quick condition keyword into actual Mongo condition keyword.
protected normalizeConditionKeyword ( string $key ) : string
$key string raw condition key.
Результат string actual key.

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

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

the MongoDB connection.
public $db