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

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

Свойство Тип Описание
$database MongoDB database instance.
$name name of this collection.

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

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

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

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

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

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

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.
Результат array inserted data, each row will have "_id" key assigned to it.

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

Counts records in this collection.
С версии: 2.1
public count ( array $condition = [], array $options = [] ) : integer
$condition array query condition
$options array list of options in format: optionName => optionValue.
Результат integer records count.

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

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.
Результат boolean whether the operation successful.

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

Example: php $collection = Yii::$app->mongo->getCollection('customer'); $collection->createIndexes([ [ 'key' => ['name'], ], [ 'key' => [ 'email' => 1, 'address' => -1, ], 'name' => 'my_index' ], ]);
С версии: 2.1
public createIndexes ( array $indexes ) : boolean
$indexes array indexes specification, each index should be specified as an array.
Результат boolean whether operation was successful.

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

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.
Результат array | boolean array of distinct values, or "false" on failure.

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

Drops this collection.
public drop ( ) : boolean
Результат boolean whether the operation successful.

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

Drops all indexes for this collection.
public dropAllIndexes ( ) : integer
Результат integer count of dropped indexes.

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

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', ] ```
Результат boolean whether the operation successful.

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

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.
Результат integer count of dropped indexes.

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

In order to perform "find" queries use Query class.
См. также: 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).
Результат MongoDB\Driver\Cursor cursor for the search results

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

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.
Результат array | null the original document, or the modified document when $options['new'] is set.

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

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).
Результат array | null the single document. Null is returned if the query results in nothing.

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

public getFullName ( ) : string
Результат string full name of this collection, including database name.

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

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.
Результат array the result of the aggregation.

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

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.
Результат MongoDB\BSON\ObjectID new record ID instance.

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

Returns the list of defined indexes.
С версии: 2.1
public listIndexes ( array $options = [] ) : array
$options array list of options in format: optionName => optionValue.
Результат array list of indexes info.

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

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.
Результат string | array the map reduce output collection name or output results.

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

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.
Результат integer | boolean number of updated documents or whether operation was successful.

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

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.
Результат MongoId updated/new record id instance.

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

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.
Результат integer | boolean number of updated documents or whether operation was successful.

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

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

MongoDB database instance.
public $database

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

name of this collection.
public $name