PHP Класс yii\mongodb\Command

A command object is usually created by calling [[Connection::createCommand()]] or [[Database::createCommand()]]. The statement it represents can be set via the [[document]] property. To execute a non-query command, such as 'listIndexes', 'count', 'distinct' and so on, call Command::execute. For example: php $result = Yii::$app->mongodb->createCommand(['listIndexes' => 'some_collection'])->execute(); To execute a 'find' command, which return cursor, call Command::query. For example: php $cursor = Yii::$app->mongodb->createCommand(['projection' => ['name' => true]])->query('some_collection'); To execute batch (bulk) operations, call Command::executeBatch. For example: php Yii::$app->mongodb->createCommand() ->addInsert(['name' => 'new']) ->addUpdate(['name' => 'existing'], ['name' => 'updated']) ->addDelete(['name' => 'old']) ->executeBatch('some_collection');
С версии: 2.1
Автор: Paul Klimov ([email protected])
Наследование: extends yii\base\Object
Показать файл Открыть проект

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

Свойство Тип Описание
$databaseName name of the database that this command is associated with.
$db the MongoDB connection that this command is associated with.
$document command document contents.

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

Метод Описание
addDelete ( array $condition, array $options = [] ) Adds the delete operation to the batch command.
addInsert ( array $document ) Adds the insert operation to the batch command.
addUpdate ( array $condition, array $document, array $options = [] ) Adds the update operation to the batch command.
aggregate ( string $collectionName, array $pipelines, array $options = [] ) : array Performs aggregation using MongoDB Aggregation Framework.
batchInsert ( string $collectionName, array[] $documents, array $options = [] ) : array | false Inserts batch of new documents into collection.
count ( string $collectionName, array $condition = [], array $options = [] ) : integer Counts records in specified collection.
createCollection ( string $collectionName, array $options = [] ) : boolean Creates new collection in database associated with this command.s
createIndexes ( string $collectionName, array[] $indexes ) : boolean Creates indexes in the collection.
delete ( string $collectionName, array $condition, array $options = [] ) : MongoDB\Driver\WriteResult Removes documents from the collection.
distinct ( string $collectionName, string $fieldName, array $condition = [], array $options = [] ) : array Returns a list of distinct values for the given column across a collection.
dropCollection ( string $collectionName ) : boolean Drops specified collection.
dropDatabase ( ) : boolean Drops database associated with this command.
dropIndexes ( string $collectionName, string $indexes ) : array Drops collection indexes by name.
execute ( ) : MongoDB\Driver\Cursor Executes this command.
executeBatch ( string $collectionName, array $options = [] ) : array Execute commands batch (bulk).
explain ( string $collectionName, array $query ) : array Return an explanation of the query, often useful for optimization and debugging.
find ( string $collectionName, array $condition, array $options = [] ) : MongoDB\Driver\Cursor Performs find query.
findAndModify ( $collectionName, array $condition = [], array $update = [], array $options = [] ) : array | null Updates a document and returns it.
getReadConcern ( ) : MongoDB\Driver\ReadConcern | string Retuns read concern for this command.
getReadPreference ( ) : MongoDB\Driver\ReadPreference Returns read preference for this command.
getWriteConcern ( ) : MongoDB\Driver\WriteConcern | null Returns write concern for this command.
group ( string $collectionName, mixed $keys, array $initial, MongoDB\BSON\Javascript | string $reduce, array $options = [] ) : array Performs aggregation using MongoDB "group" command.
insert ( string $collectionName, array $document, array $options = [] ) : MongoDB\BSON\ObjectID | boolean Inserts new document into collection.
listCollections ( array $condition = [], array $options = [] ) : array Returns the list of available collections.
listDatabases ( array $condition = [], array $options = [] ) : array Returns the list of available databases.
listIndexes ( string $collectionName, array $options = [] ) : array Returns information about current collection indexes.
mapReduce ( string $collectionName, MongoDB\BSON\Javascript | string $map, MongoDB\BSON\Javascript | string $reduce, string | array $out, array $condition = [], array $options = [] ) : string | array Performs MongoDB "map-reduce" command.
query ( string $collectionName, array $options = [] ) : MongoDB\Driver\Cursor Executes this command as a mongo query
setReadConcern ( MongoDB\Driver\ReadConcern | string $readConcern ) Sets read concern for this command.
setReadPreference ( MongoDB\Driver\ReadPreference | integer | string | null $readPreference ) Sets read preference for this command.
setWriteConcern ( MongoDB\Driver\WriteConcern | integer | string | null $writeConcern ) Sets write concern for this command.
update ( string $collectionName, array $condition, array $document, array $options = [] ) : MongoDB\Driver\WriteResult Update existing documents in the collection.

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

Метод Описание
beginProfile ( string $token, string $category ) Marks the beginning of a code block for profiling.
endProfile ( string $token, string $category ) Marks the end of a code block for profiling.
log ( array | string $namespace, array $data, string $category ) : string | false Logs the command data if logging is enabled at [[db]].

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

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

Adds the delete operation to the batch command.
См. также: executeBatch()
public addDelete ( array $condition, array $options = [] )
$condition array filter condition.
$options array delete options.

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

Adds the insert operation to the batch command.
См. также: executeBatch()
public addInsert ( array $document )
$document array document to be inserted

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

Adds the update operation to the batch command.
См. также: executeBatch()
public addUpdate ( array $condition, array $document, array $options = [] )
$condition array filter condition
$document array data to be updated
$options array update options.

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

Performs aggregation using MongoDB Aggregation Framework.
public aggregate ( string $collectionName, array $pipelines, array $options = [] ) : array
$collectionName string collection name
$pipelines array list of pipeline operators.
$options array optional parameters.
Результат array aggregation result.

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

Inserts batch of new documents into collection.
public batchInsert ( string $collectionName, array[] $documents, array $options = [] ) : array | false
$collectionName string collection name
$documents array[] documents list
$options array list of options in format: optionName => optionValue.
Результат array | false list of inserted IDs, `false` on failure.

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

Marks the beginning of a code block for profiling.
См. также: endProfile()
protected beginProfile ( string $token, string $category )
$token string token for the code block
$category string the category of this log message

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

Counts records in specified collection.
public count ( string $collectionName, array $condition = [], array $options = [] ) : integer
$collectionName string collection name
$condition array filter condition
$options array list of options in format: optionName => optionValue.
Результат integer records count

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

Creates new collection in database associated with this command.s
public createCollection ( string $collectionName, array $options = [] ) : boolean
$collectionName string collection name
$options array collection options in format: "name" => "value"
Результат boolean whether operation was successful.

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

Creates indexes in the collection.
public createIndexes ( string $collectionName, array[] $indexes ) : boolean
$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.
Результат boolean whether operation was successful.

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

Removes documents from the collection.
public delete ( string $collectionName, array $condition, array $options = [] ) : MongoDB\Driver\WriteResult
$collectionName string collection name.
$condition array filter condition.
$options array delete options.
Результат MongoDB\Driver\WriteResult write result.

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

Returns a list of distinct values for the given column across a collection.
public distinct ( string $collectionName, string $fieldName, array $condition = [], array $options = [] ) : array
$collectionName string collection name.
$fieldName string field name to use.
$condition array query parameters.
$options array list of options in format: optionName => optionValue.
Результат array array of distinct values, or "false" on failure.

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

Drops specified collection.
public dropCollection ( string $collectionName ) : boolean
$collectionName string name of the collection to be dropped.
Результат boolean whether operation was successful.

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

Drops database associated with this command.
public dropDatabase ( ) : boolean
Результат boolean whether operation was successful.

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

Drops collection indexes by name.
public dropIndexes ( string $collectionName, string $indexes ) : array
$collectionName string collection name.
$indexes string wildcard for name of the indexes to be dropped.
Результат array result data.

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

Marks the end of a code block for profiling.
См. также: beginProfile()
protected endProfile ( string $token, string $category )
$token string token for the code block
$category string the category of this log message

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

Executes this command.
public execute ( ) : MongoDB\Driver\Cursor
Результат MongoDB\Driver\Cursor result cursor.

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

Execute commands batch (bulk).
public executeBatch ( string $collectionName, array $options = [] ) : array
$collectionName string collection name.
$options array batch options.
Результат array array of 2 elements: - 'insertedIds' - contains inserted IDs. - 'result' - [[\MongoDB\Driver\WriteResult]] instance.

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

Return an explanation of the query, often useful for optimization and debugging.
public explain ( string $collectionName, array $query ) : array
$collectionName string collection name
$query array query document.
Результат array explanation of the query.

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

Performs find query.
public find ( string $collectionName, array $condition, array $options = [] ) : MongoDB\Driver\Cursor
$collectionName string collection name
$condition array filter condition
$options array query options.
Результат MongoDB\Driver\Cursor result cursor.

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

Updates a document and returns it.
public findAndModify ( $collectionName, array $condition = [], array $update = [], array $options = [] ) : array | null
$collectionName
$condition array query condition
$update array update criteria
$options array list of options in format: optionName => optionValue.
Результат array | null the original document, or the modified document when $options['new'] is set.

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

Retuns read concern for this command.
public getReadConcern ( ) : MongoDB\Driver\ReadConcern | string
Результат MongoDB\Driver\ReadConcern | string read concern to be used in this command.

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

Returns read preference for this command.
public getReadPreference ( ) : MongoDB\Driver\ReadPreference
Результат MongoDB\Driver\ReadPreference read preference.

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

Returns write concern for this command.
public getWriteConcern ( ) : MongoDB\Driver\WriteConcern | null
Результат MongoDB\Driver\WriteConcern | null write concern to be used in this command.

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

Performs aggregation using MongoDB "group" command.
public group ( string $collectionName, mixed $keys, array $initial, MongoDB\BSON\Javascript | string $reduce, array $options = [] ) : array
$collectionName string collection name.
$keys mixed fields to group by. If an array or non-code object is passed, it will be the key used to group results. If instance of [[\MongoDB\BSON\Javascript]] passed, it will be treated as a function that returns the key to group by.
$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 [[\MongoDB\BSON\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 the result of the aggregation.

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

Inserts new document into collection.
public insert ( string $collectionName, array $document, array $options = [] ) : MongoDB\BSON\ObjectID | boolean
$collectionName string collection name
$document array document content
$options array list of options in format: optionName => optionValue.
Результат MongoDB\BSON\ObjectID | boolean inserted record ID, `false` - on failure.

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

Returns the list of available collections.
public listCollections ( array $condition = [], array $options = [] ) : array
$condition array filter condition.
$options array options list.
Результат array collections information.

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

Returns the list of available databases.
public listDatabases ( array $condition = [], array $options = [] ) : array
$condition array filter condition.
$options array options list.
Результат array database information

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

Returns information about current collection indexes.
public listIndexes ( string $collectionName, array $options = [] ) : array
$collectionName string collection name
$options array list of options in format: optionName => optionValue.
Результат array list of indexes info.

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

Logs the command data if logging is enabled at [[db]].
protected log ( array | string $namespace, array $data, string $category ) : string | false
$namespace array | string command namespace.
$data array command data.
$category string log category
Результат string | false log token, `false` if log is not enabled.

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

Performs MongoDB "map-reduce" command.
public mapReduce ( string $collectionName, MongoDB\BSON\Javascript | string $map, MongoDB\BSON\Javascript | string $reduce, string | array $out, array $condition = [], array $options = [] ) : string | 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.
Результат string | array the map reduce output collection name or output results.

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

Executes this command as a mongo query
public query ( string $collectionName, array $options = [] ) : MongoDB\Driver\Cursor
$collectionName string collection name
$options array query options.
Результат MongoDB\Driver\Cursor result cursor.

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

Sets read concern for this command.
public setReadConcern ( MongoDB\Driver\ReadConcern | string $readConcern )
$readConcern MongoDB\Driver\ReadConcern | string read concern, it can be an instance of [[ReadConcern]] or scalar level value, for example: 'local'.

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

Sets read preference for this command.
public setReadPreference ( MongoDB\Driver\ReadPreference | integer | string | null $readPreference )
$readPreference MongoDB\Driver\ReadPreference | integer | string | null read reference, it can be specified as instance of [[ReadPreference]] or scalar mode value, for example: `ReadPreference::RP_PRIMARY`.

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

Sets write concern for this command.
public setWriteConcern ( MongoDB\Driver\WriteConcern | integer | string | null $writeConcern )
$writeConcern MongoDB\Driver\WriteConcern | integer | string | null write concern, it can be an instance of [[WriteConcern]] or its scalar mode value, for example: `majority`.

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

Update existing documents in the collection.
public update ( string $collectionName, array $condition, array $document, array $options = [] ) : MongoDB\Driver\WriteResult
$collectionName string collection name
$condition array filter condition
$document array data to be updated.
$options array update options.
Результат MongoDB\Driver\WriteResult write result.

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

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

name of the database that this command is associated with.
public $databaseName

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

the MongoDB connection that this command is associated with.
public $db

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

command document contents.
public $document