PHP Class yii\mongodb\Collection

A collection object is usually created by calling [[Database::getCollection()]] or [[Connection::getCollection()]]. Collection provides the basic interface for the Mongo queries, mostly: insert, update, delete operations. For example: php $collection = Yii::$app->mongodb->getCollection('customer'); $collection->insert(['name' => 'John Smith', 'status' => 1]); Collection also provides shortcut for Command methods, such as Collection::group, Collection::mapReduce and so on. To perform "find" queries, please use Query instead.
Since: 2.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Object
ファイルを表示 Open project: yiisoft/yii2-mongodb Class Usage Examples

Public Properties

Property Type Description
$database MongoDB database instance.
$name name of this collection.

Public Methods

Method Description
aggregate ( array $pipelines, array $options = [] ) : array Performs aggregation using Mongo Aggregation Framework.
batchInsert ( array $rows, array $options = [] ) : array Inserts several new rows into collection.
count ( array $condition = [], array $options = [] ) : integer Counts records in this collection.
createIndex ( array | string $columns, array $options = [] ) : boolean Creates an index on the collection and the specified fields.
createIndexes ( array $indexes ) : boolean Creates several indexes at once.
distinct ( string $column, array $condition = [], array $options = [] ) : array | boolean Returns a list of distinct values for the given column across a collection.
drop ( ) : boolean Drops this collection.
dropAllIndexes ( ) : integer Drops all indexes for this collection.
dropIndex ( string | array $columns ) : boolean Drop indexes for specified column(s).
dropIndexes ( string $indexes ) : integer Drops collection indexes by name.
find ( array $condition = [], array $fields = [], array $options = [] ) : MongoDB\Driver\Cursor Returns a cursor for the search results.
findAndModify ( array $condition, array $update, array $options = [] ) : array | null Updates a document and returns it.
findOne ( array $condition = [], array $fields = [], array $options = [] ) : array | null Returns a single document.
getFullName ( ) : string
group ( mixed $keys, array $initial, MongoDB\BSON\Javascript | string $reduce, array $options = [] ) : array Performs aggregation using Mongo "group" command.
insert ( array | object $data, array $options = [] ) : MongoDB\BSON\ObjectID Inserts new data into collection.
listIndexes ( array $options = [] ) : array Returns the list of defined indexes.
mapReduce ( MongoDB\BSON\Javascript | string $map, MongoDB\BSON\Javascript | string $reduce, string | array $out, array $condition = [], array $options = [] ) : string | array Performs aggregation using MongoDB "map-reduce" mechanism.
remove ( array $condition = [], array $options = [] ) : integer | boolean Removes data from the collection.
save ( array | object $data, array $options = [] ) : MongoId Update the existing database data, otherwise insert this data
update ( array $condition, array $newData, array $options = [] ) : integer | boolean Updates the rows, which matches given criteria by given data.

Method Details

aggregate() public method

Performs aggregation using Mongo Aggregation Framework.
public aggregate ( array $pipelines, array $options = [] ) : array
$pipelines array list of pipeline operators.
$options array optional parameters.
return array the result of the aggregation.

batchInsert() public method

Inserts several new rows into collection.
public batchInsert ( array $rows, array $options = [] ) : array
$rows array array of arrays or objects to be inserted.
$options array list of options in format: optionName => optionValue.
return array inserted data, each row will have "_id" key assigned to it.

count() public method

Counts records in this collection.
Since: 2.1
public count ( array $condition = [], array $options = [] ) : integer
$condition array query condition
$options array list of options in format: optionName => optionValue.
return integer records count.

createIndex() public method

Creates an index on the collection and the specified fields.
public createIndex ( array | string $columns, array $options = [] ) : boolean
$columns array | string column name or list of column names. If array is given, each element in the array has as key the field name, and as value either 1 for ascending sort, or -1 for descending sort. You can specify field using native numeric key with the field name as a value, in this case ascending sort will be used. For example: ```php [ 'name', 'status' => -1, ] ```
$options array list of options in format: optionName => optionValue.
return boolean whether the operation successful.

createIndexes() public method

Example: php $collection = Yii::$app->mongo->getCollection('customer'); $collection->createIndexes([ [ 'key' => ['name'], ], [ 'key' => [ 'email' => 1, 'address' => -1, ], 'name' => 'my_index' ], ]);
Since: 2.1
public createIndexes ( array $indexes ) : boolean
$indexes array indexes specification, each index should be specified as an array.
return boolean whether operation was successful.

distinct() public method

Returns a list of distinct values for the given column across a collection.
public distinct ( string $column, array $condition = [], array $options = [] ) : array | boolean
$column string column to use.
$condition array query parameters.
$options array list of options in format: optionName => optionValue.
return array | boolean array of distinct values, or "false" on failure.

drop() public method

Drops this collection.
public drop ( ) : boolean
return boolean whether the operation successful.

dropAllIndexes() public method

Drops all indexes for this collection.
public dropAllIndexes ( ) : integer
return integer count of dropped indexes.

dropIndex() public method

Drop indexes for specified column(s).
public dropIndex ( string | array $columns ) : boolean
$columns string | array column name or list of column names. If array is given, each element in the array has as key the field name, and as value either 1 for ascending sort, or -1 for descending sort. Use value 'text' to specify text index. You can specify field using native numeric key with the field name as a value, in this case ascending sort will be used. For example: ```php [ 'name', 'status' => -1, 'description' => 'text', ] ```
return boolean whether the operation successful.

dropIndexes() public method

Drops collection indexes by name.
public dropIndexes ( string $indexes ) : integer
$indexes string wildcard for name of the indexes to be dropped. You can use `*` to drop all indexes.
return integer count of dropped indexes.

find() public method

In order to perform "find" queries use Query class.
See also: Query
public find ( array $condition = [], array $fields = [], array $options = [] ) : MongoDB\Driver\Cursor
$condition array query condition
$fields array fields to be selected
$options array query options (available since 2.1).
return MongoDB\Driver\Cursor cursor for the search results

findAndModify() public method

Updates a document and returns it.
public findAndModify ( array $condition, array $update, array $options = [] ) : array | null
$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.

findOne() public method

Returns a single document.
public findOne ( array $condition = [], array $fields = [], array $options = [] ) : array | null
$condition array query condition
$fields array fields to be selected
$options array query options (available since 2.1).
return array | null the single document. Null is returned if the query results in nothing.

getFullName() public method

public getFullName ( ) : string
return string full name of this collection, including database name.

group() public method

Performs aggregation using Mongo "group" command.
public group ( mixed $keys, array $initial, MongoDB\BSON\Javascript | string $reduce, array $options = [] ) : array
$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 data into collection.
public insert ( array | object $data, array $options = [] ) : MongoDB\BSON\ObjectID
$data array | object data to be inserted.
$options array list of options in format: optionName => optionValue.
return MongoDB\BSON\ObjectID new record ID instance.

listIndexes() public method

Returns the list of defined indexes.
Since: 2.1
public listIndexes ( array $options = [] ) : array
$options array list of options in format: optionName => optionValue.
return array list of indexes info.

mapReduce() public method

Note: this function will not return the aggregation result, instead it will write it inside the another Mongo collection specified by "out" parameter. For example: php $customerCollection = Yii::$app->mongo->getCollection('customer'); $resultCollectionName = $customerCollection->mapReduce( 'function () {emit(this.status, this.amount)}', 'function (key, values) {return Array.sum(values)}', 'mapReduceOut', ['status' => 3] ); $query = new Query(); $results = $query->from($resultCollectionName)->all();
public mapReduce ( MongoDB\BSON\Javascript | string $map, MongoDB\BSON\Javascript | string $reduce, string | array $out, array $condition = [], array $options = [] ) : string | array
$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 criteria 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.

remove() public method

Removes data from the collection.
public remove ( array $condition = [], array $options = [] ) : integer | boolean
$condition array description of records to remove.
$options array list of options in format: optionName => optionValue.
return integer | boolean number of updated documents or whether operation was successful.

save() public method

Update the existing database data, otherwise insert this data
public save ( array | object $data, array $options = [] ) : MongoId
$data array | object data to be updated/inserted.
$options array list of options in format: optionName => optionValue.
return MongoId updated/new record id instance.

update() public method

Note: for "multi" mode Mongo requires explicit strategy "$set" or "$inc" to be specified for the "newData". If no strategy is passed "$set" will be used.
public update ( array $condition, array $newData, array $options = [] ) : integer | boolean
$condition array description of the objects to update.
$newData array the object with which to update the matching records.
$options array list of options in format: optionName => optionValue.
return integer | boolean number of updated documents or whether operation was successful.

Property Details

$database public_oe property

MongoDB database instance.
public $database

$name public_oe property

name of this collection.
public $name