PHP Class 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');
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
$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.

Public Methods

Method Description
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.

Protected Methods

Method Description
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]].

Method Details

addDelete() public method

Adds the delete operation to the batch command.
See also: executeBatch()
public addDelete ( array $condition, array $options = [] )
$condition array filter condition.
$options array delete options.

addInsert() public method

Adds the insert operation to the batch command.
See also: executeBatch()
public addInsert ( array $document )
$document array document to be inserted

addUpdate() public method

Adds the update operation to the batch command.
See also: executeBatch()
public addUpdate ( array $condition, array $document, array $options = [] )
$condition array filter condition
$document array data to be updated
$options array update options.

aggregate() public method

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.
return array aggregation result.

batchInsert() public method

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.
return array | false list of inserted IDs, `false` on failure.

beginProfile() protected method

Marks the beginning of a code block for profiling.
See also: endProfile()
protected beginProfile ( string $token, string $category )
$token string token for the code block
$category string the category of this log message

count() public method

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.
return integer records count

createCollection() public method

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"
return boolean whether operation was successful.

createIndexes() public method

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.
return boolean whether operation was successful.

delete() public method

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.
return MongoDB\Driver\WriteResult write result.

distinct() public method

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.
return array array of distinct values, or "false" on failure.

dropCollection() public method

Drops specified collection.
public dropCollection ( string $collectionName ) : boolean
$collectionName string name of the collection to be dropped.
return boolean whether operation was successful.

dropDatabase() public method

Drops database associated with this command.
public dropDatabase ( ) : boolean
return boolean whether operation was successful.

dropIndexes() public method

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.
return array result data.

endProfile() protected method

Marks the end of a code block for profiling.
See also: beginProfile()
protected endProfile ( string $token, string $category )
$token string token for the code block
$category string the category of this log message

execute() public method

Executes this command.
public execute ( ) : MongoDB\Driver\Cursor
return MongoDB\Driver\Cursor result cursor.

executeBatch() public method

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

explain() public method

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.
return array explanation of the query.

find() public method

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.
return MongoDB\Driver\Cursor result cursor.

findAndModify() public method

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

getReadConcern() public method

Retuns read concern for this command.
public getReadConcern ( ) : MongoDB\Driver\ReadConcern | string
return MongoDB\Driver\ReadConcern | string read concern to be used in this command.

getReadPreference() public method

Returns read preference for this command.
public getReadPreference ( ) : MongoDB\Driver\ReadPreference
return MongoDB\Driver\ReadPreference read preference.

getWriteConcern() public method

Returns write concern for this command.
public getWriteConcern ( ) : MongoDB\Driver\WriteConcern | null
return MongoDB\Driver\WriteConcern | null write concern to be used in this command.

group() public method

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.
return array the result of the aggregation.

insert() public method

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.
return MongoDB\BSON\ObjectID | boolean inserted record ID, `false` - on failure.

listCollections() public method

Returns the list of available collections.
public listCollections ( array $condition = [], array $options = [] ) : array
$condition array filter condition.
$options array options list.
return array collections information.

listDatabases() public method

Returns the list of available databases.
public listDatabases ( array $condition = [], array $options = [] ) : array
$condition array filter condition.
$options array options list.
return array database information

listIndexes() public method

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.
return array list of indexes info.

log() protected method

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
return string | false log token, `false` if log is not enabled.

mapReduce() public method

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.
return string | array the map reduce output collection name or output results.

query() public method

Executes this command as a mongo query
public query ( string $collectionName, array $options = [] ) : MongoDB\Driver\Cursor
$collectionName string collection name
$options array query options.
return MongoDB\Driver\Cursor result cursor.

setReadConcern() public method

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() public method

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() public method

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() public method

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.
return MongoDB\Driver\WriteResult write result.

Property Details

$databaseName public_oe property

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

$db public_oe property

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

$document public_oe property

command document contents.
public $document