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
파일 보기 프로젝트 열기: yiisoft/yii2-mongodb 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$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