PHP Class Sokil\Mongo\Collection

It aggregates \MongoCollection instance.
Inheritance: implements Countable
Show file Open project: sokil/php-mongo Class Usage Examples

Protected Properties

Property Type Description
$_index Every index definition must contain key 'keys' with list of fields and orders, and optional options from @link http://php.net/manual/en/mongocollection.createindex.php: Example: array( array( 'keys' => array('field1' => 1, 'field2' => -1), 'unique' => true ), ... )
$_queryExpressionClass expression class. This class may be overloaded to define own query methods (whereUserAgeGreatedThan(), etc.)
$mongoCollectionClassName
$versioning default value of versioning

Public Methods

Method Description
__construct ( Database $database, $collection, Definition $definition = null )
__get ( $name )
addDocumentToDocumentPool ( Document $document ) : Collection Store document to pool
addDocumentsToDocumentPool ( array $documents ) : Collection Store documents to identity map
aggregate ( callable | array | Pipeline $pipeline, array $options = [], boolean $asCursor = false ) : array Aggregate using pipeline
batchDelete ( callable | array | Expression $expression = [] ) : Collection Delete documents by expression
batchInsert ( array $rows, $validate = true ) : Collection Insert multiple documents defined as arrays
batchUpdate ( Expression | array | callable $expression, Operator | array | callable $updateData ) : Collection Update multiple documents
clearDocumentPool ( )
count ( ) : integer Total count of documents in collection
createAggregator ( ) : Pipeline Start aggregation
createBatchDelete ( integer | string $writeConcern = null, integer $timeout = null, boolean $ordered = null ) : Sokil\Mongo\BatchDelete Creates batch delete operation handler
createBatchInsert ( integer | string $writeConcern = null, integer $timeout = null, boolean $ordered = null ) : Sokil\Mongo\BatchInsert Creates batch insert operation handler
createBatchUpdate ( integer | string $writeConcern = null, integer $timeout = null, boolean $ordered = null ) : Sokil\Mongo\BatchUpdate Creates batch update operation handler
createDocument ( array $data = null ) : Document Factory method to get not stored Document instance from array
createIndex ( array $key, array $options = [] ) : Collection Create index
delete ( ) : Collection Delete collection
deleteDocument ( Document $document ) : Collection
deleteDocuments ( $expression = [] )
deleteIndex ( array $key ) : Collection Delete index
disableDocumentPool ( ) : Collection Stop storing found documents to pool
documentPoolCount ( ) : integer Get number of documents in document pool
enableDocumentPool ( ) : Collection Start storing found documents to pool
enableVersioning ( ) : Collection Start versioning documents on modify
ensure2dIndex ( string $field ) : Collection Create geo index 2dsphere
ensure2dSphereIndex ( string $field ) : Collection Create geo index 2dsphere
ensureFulltextIndex ( array | string $field = '$**', array $weights = null, $defaultLanguage = Language::ENGLISH, $languageOverride = null ) : Collection Create fulltext index
ensureIndex ( array $key, array $options = [] ) : Collection Create index
ensureSparseIndex ( array $key ) : Collection Create sparse index.
ensureTTLIndex ( array $key, integer $seconds ) : Collection Create TTL index
ensureUniqueIndex ( array $key, boolean $dropDups = false ) : Collection Create unique index
explainAggregate ( array | Pipeline $pipeline ) : array Explain aggregation
expression ( ) : Expression Create new Expression instance to use in query builder or update operations
find ( $callable = null ) : Cursor | Expression Create document query builder
findAsArray ( $callable = null ) : Cursor Create document query builder
getDatabase ( ) : Database
getDistinct ( string $selector, array | callable | Expression $expression = null ) : array Retrieve a list of distinct values for the given key across a collection.
getDocument ( string | MongoId $id, callable $callable = null ) : Document | array | null Get document by id If callable specified, document always loaded directly omitting document pool.
getDocumentByReference ( array $ref, boolean $useDocumentPool = true ) : Document | null Get Document instance by it's reference
getDocumentClassName ( array $documentData = null ) : string Override to define class name of document by document data
getDocumentDirectly ( string | MongoId $id, callable $callable = null ) : Document | array | null Get document by id directly omitting cache Method may return document as array if cursor configured through Cursor::asArray()
getDocumentFromDocumentPool ( string | integer | MongoId $id ) : Document Get document from identity map by it's id
getDocuments ( array $idList, callable $callable = null ) : array | null Get documents by list of id
getDocumentsFromDocumentPool ( array $ids = null ) Get documents from pool if they stored
getIndexes ( ) : array Get index info
getMongoCollection ( ) : MongoCollection Get native collection instance of mongo driver
getName ( ) : string Get name of collection
getOption ( string | integer $name ) : mixed Get option
getOptions ( )
getReadPreference ( )
getWriteConcern ( ) : integer | string Get currently active write concern on all requests to collection
hasDocument ( Document $document ) : type Check if document belongs to collection
hydrate ( array $data, $useDocumentPool = true ) : Document Factory method to get document object from array of stored document
initIndexes ( ) : Collection Create indexes based on self::$_index metadata
insert ( array $document ) : Collection Direct insert of array to MongoDB without creating document object and validation
insertMultiple ( $rows, $validate = true )
isDocumentInDocumentPool ( Document | MongoId | integer | string $document ) : boolean Check if document exists in identity map
isDocumentPoolEmpty ( ) : boolean Check if documents are in pool
isDocumentPoolEnabled ( ) : boolean Check if document pool enabled and requested documents store to it
isVersioningEnabled ( ) : boolean Check if versioning enabled
operator ( ) : Operator Create Operator instance to use in update operations
readNearest ( array $tags = null )
readPrimaryOnly ( )
readPrimaryPreferred ( array $tags = null )
readSecondaryOnly ( array $tags = null )
readSecondaryPreferred ( array $tags = null )
removeDocumentFromDocumentPool ( Document $document ) : Collection Remove document instance from identity map
setMajorityWriteConcern ( integer $timeout = 10000 ) : Collection Define majority write concern for all requests to current collection
setUnacknowledgedWriteConcern ( integer $timeout = 10000 ) : Collection Define unacknowledged write concern for all requests to current collection
setWriteConcern ( string | integer $w, integer $timeout = 10000 ) : Collection Define write concern for all requests to current collection
stats ( ) : array Get collection stat
update ( Expression | array | callable $expression, Operator | array | callable $updateData, array $options = [] ) : Collection Update multiple documents
updateAll ( Operator | array | callable $updateData ) : Collection Update all documents
updateMultiple ( $expression, $updateData )
validate ( boolean $full = false ) : array Validates a collection. The method scans a collection’s data structures for correctness and returns a single document that describes the relationship between the logical collection and the physical representation of the data.

Method Details

__construct() public method

public __construct ( Database $database, $collection, Definition $definition = null )
$database Database
$definition Sokil\Mongo\Collection\Definition

__get() public method

public __get ( $name )

addDocumentToDocumentPool() public method

Store document to pool
public addDocumentToDocumentPool ( Document $document ) : Collection
$document Document
return Collection

addDocumentsToDocumentPool() public method

Store documents to identity map
public addDocumentsToDocumentPool ( array $documents ) : Collection
$documents array list of Document instances
return Collection

aggregate() public method

Aggregate using pipeline
public aggregate ( callable | array | Pipeline $pipeline, array $options = [], boolean $asCursor = false ) : array
$pipeline callable | array | Pipeline list of pipeline stages
$options array
$asCursor boolean return result as cursor
return array result of aggregation

batchDelete() public method

Delete documents by expression
public batchDelete ( callable | array | Expression $expression = [] ) : Collection
$expression callable | array | Expression
return Collection

batchInsert() public method

Prior to version 1.5.0 of the driver it was possible to use MongoCollection::batchInsert(), however, as of 1.5.0 that method is now discouraged. You can use Collection::createBatchInsert()
public batchInsert ( array $rows, $validate = true ) : Collection
$rows array list of documents to insert, defined as arrays
return Collection

batchUpdate() public method

Update multiple documents
public batchUpdate ( Expression | array | callable $expression, Operator | array | callable $updateData ) : Collection
$expression Expression | array | callable expression to define which documents will change.
$updateData Operator | array | callable new data or operators to update
return Collection

clearDocumentPool() public method

public clearDocumentPool ( )

count() public method

Total count of documents in collection
public count ( ) : integer
return integer

createAggregator() public method

Start aggregation
public createAggregator ( ) : Pipeline
return Pipeline

createBatchDelete() public method

Creates batch delete operation handler
public createBatchDelete ( integer | string $writeConcern = null, integer $timeout = null, boolean $ordered = null ) : Sokil\Mongo\BatchDelete
$writeConcern integer | string Write concern. Default is 1 (Acknowledged)
$timeout integer Timeout for write concern. Default is 10000 milliseconds
$ordered boolean Determins if MongoDB must apply this batch in order (sequentally, one item at a time) or can rearrange it. Defaults to TRUE
return Sokil\Mongo\BatchDelete

createBatchInsert() public method

Creates batch insert operation handler
public createBatchInsert ( integer | string $writeConcern = null, integer $timeout = null, boolean $ordered = null ) : Sokil\Mongo\BatchInsert
$writeConcern integer | string Write concern. Default is 1 (Acknowledged)
$timeout integer Timeout for write concern. Default is 10000 milliseconds
$ordered boolean Determins if MongoDB must apply this batch in order (sequentally, one item at a time) or can rearrange it. Defaults to TRUE
return Sokil\Mongo\BatchInsert

createBatchUpdate() public method

Creates batch update operation handler
public createBatchUpdate ( integer | string $writeConcern = null, integer $timeout = null, boolean $ordered = null ) : Sokil\Mongo\BatchUpdate
$writeConcern integer | string Write concern. Default is 1 (Acknowledged)
$timeout integer Timeout for write concern. Default is 10000 milliseconds
$ordered boolean Determins if MongoDB must apply this batch in order (sequentally, one item at a time) or can rearrange it. Defaults to TRUE
return Sokil\Mongo\BatchUpdate

createDocument() public method

Factory method to get not stored Document instance from array
public createDocument ( array $data = null ) : Document
$data array
return Document

createIndex() public method

Create index
public createIndex ( array $key, array $options = [] ) : Collection
$key array
$options array see @link http://php.net/manual/en/mongocollection.ensureindex.php
return Collection

delete() public method

Delete collection
public delete ( ) : Collection
return Collection

deleteDocument() public method

Deprecation: since 1.13. Use Document::delete()
public deleteDocument ( Document $document ) : Collection
$document Document
return Collection

deleteDocuments() public method

Deprecation: since 1.13. Use Collection::batchDelete();
public deleteDocuments ( $expression = [] )

deleteIndex() public method

Delete index
public deleteIndex ( array $key ) : Collection
$key array
return Collection

disableDocumentPool() public method

Stop storing found documents to pool
public disableDocumentPool ( ) : Collection
return Collection

documentPoolCount() public method

Get number of documents in document pool
public documentPoolCount ( ) : integer
return integer

enableDocumentPool() public method

Start storing found documents to pool
public enableDocumentPool ( ) : Collection
return Collection

enableVersioning() public method

Start versioning documents on modify
Deprecation: since 1.13 Use 'versioning' declaration in mapping
public enableVersioning ( ) : Collection
return Collection

ensure2dIndex() public method

Create geo index 2dsphere
public ensure2dIndex ( string $field ) : Collection
$field string
return Collection

ensure2dSphereIndex() public method

Create geo index 2dsphere
public ensure2dSphereIndex ( string $field ) : Collection
$field string
return Collection

ensureFulltextIndex() public method

Create fulltext index
public ensureFulltextIndex ( array | string $field = '$**', array $weights = null, $defaultLanguage = Language::ENGLISH, $languageOverride = null ) : Collection
$field array | string definition of fields where full text index ensured. May be string to ensure index on one field, array of fields to create full text index on few fields, and widdcard '$**' to create index on all fields of collection. Default value is '$**'
$weights array For a text index, the weight of an indexed field denotes the significance of the field relative to the other indexed fields in terms of the text search score.
$defaultLanguage The default language associated with the indexed data determines the rules to parse word roots (i.e. stemming) and ignore stop words. The default language for the indexed data is english.
$languageOverride To use a field with a name other than language, include the language_override option when creating the index.
return Collection

ensureIndex() public method

Create index
Deprecation: since 1.19 Use self::createIndex()
public ensureIndex ( array $key, array $options = [] ) : Collection
$key array
$options array see @link http://php.net/manual/en/mongocollection.ensureindex.php
return Collection

ensureSparseIndex() public method

Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value. The index skips over any document that is missing the indexed field.
public ensureSparseIndex ( array $key ) : Collection
$key array An array specifying the index's fields as its keys. For each field, the value is either the index direction or index type. If specifying direction, specify 1 for ascending or -1 for descending.
return Collection

ensureTTLIndex() public method

Create TTL index
public ensureTTLIndex ( array $key, integer $seconds ) : Collection
$key array key must be date to use TTL
$seconds integer
return Collection

ensureUniqueIndex() public method

Create unique index
public ensureUniqueIndex ( array $key, boolean $dropDups = false ) : Collection
$key array
$dropDups boolean
return Collection

explainAggregate() public method

Explain aggregation
Deprecation: use pipeline option 'explain' in Collection::aggregate() or method Pipeline::explain()
public explainAggregate ( array | Pipeline $pipeline ) : array
$pipeline array | Pipeline
return array result

expression() public method

Create new Expression instance to use in query builder or update operations
public expression ( ) : Expression
return Expression

find() public method

Create document query builder
public find ( $callable = null ) : Cursor | Expression
$callable callable|null Function to configure query builder&
return Cursor | Expression

findAsArray() public method

Create document query builder
public findAsArray ( $callable = null ) : Cursor
return Cursor

getDatabase() public method

public getDatabase ( ) : Database
return Database

getDistinct() public method

Retrieve a list of distinct values for the given key across a collection.
public getDistinct ( string $selector, array | callable | Expression $expression = null ) : array
$selector string field selector
$expression array | callable | Expression expression to search documents
return array distinct values

getDocument() public method

Method may return document as array if cursor configured through Cursor::asArray()
public getDocument ( string | MongoId $id, callable $callable = null ) : Document | array | null
$id string | MongoId
$callable callable cursor callable used to configure cursor
return Document | array | null

getDocumentByReference() public method

Get Document instance by it's reference
public getDocumentByReference ( array $ref, boolean $useDocumentPool = true ) : Document | null
$ref array reference to document
$useDocumentPool boolean try to get document from pool or fetch document from database
return Document | null

getDocumentClassName() public method

Override to define class name of document by document data
public getDocumentClassName ( array $documentData = null ) : string
$documentData array
return string Document class data

getDocumentDirectly() public method

Get document by id directly omitting cache Method may return document as array if cursor configured through Cursor::asArray()
public getDocumentDirectly ( string | MongoId $id, callable $callable = null ) : Document | array | null
$id string | MongoId
$callable callable cursor callable used to configure cursor
return Document | array | null

getDocumentFromDocumentPool() public method

Get document from identity map by it's id
public getDocumentFromDocumentPool ( string | integer | MongoId $id ) : Document
$id string | integer | MongoId
return Document

getDocuments() public method

Get documents by list of id
public getDocuments ( array $idList, callable $callable = null ) : array | null
$idList array list of ids
$callable callable cursor callable used to configure cursor
return array | null

getDocumentsFromDocumentPool() public method

Get documents from pool if they stored
public getDocumentsFromDocumentPool ( array $ids = null )
$ids array

getIndexes() public method

Get index info
public getIndexes ( ) : array
return array

getMongoCollection() public method

Get native collection instance of mongo driver
public getMongoCollection ( ) : MongoCollection
return MongoCollection

getName() public method

Get name of collection
public getName ( ) : string
return string name of collection

getOption() public method

Get option
public getOption ( string | integer $name ) : mixed
$name string | integer
return mixed

getOptions() public method

public getOptions ( )

getReadPreference() public method

public getReadPreference ( )

getWriteConcern() public method

Get currently active write concern on all requests to collection
public getWriteConcern ( ) : integer | string
return integer | string write concern

hasDocument() public method

Check if document belongs to collection
public hasDocument ( Document $document ) : type
$document Document
return type

hydrate() public method

Factory method to get document object from array of stored document
public hydrate ( array $data, $useDocumentPool = true ) : Document
$data array
return Document

initIndexes() public method

Create indexes based on self::$_index metadata
public initIndexes ( ) : Collection
return Collection

insert() public method

Direct insert of array to MongoDB without creating document object and validation
public insert ( array $document ) : Collection
$document array
return Collection

insertMultiple() public method

Deprecation: since 1.13 Use Collection::batchInsert()
public insertMultiple ( $rows, $validate = true )

isDocumentInDocumentPool() public method

Check if document exists in identity map
public isDocumentInDocumentPool ( Document | MongoId | integer | string $document ) : boolean
$document Document | MongoId | integer | string Document instance or it's id
return boolean

isDocumentPoolEmpty() public method

Check if documents are in pool
public isDocumentPoolEmpty ( ) : boolean
return boolean

isDocumentPoolEnabled() public method

Check if document pool enabled and requested documents store to it
public isDocumentPoolEnabled ( ) : boolean
return boolean

isVersioningEnabled() public method

Check if versioning enabled
Deprecation: since 1.13 Use 'versioning' declaration in mapping
public isVersioningEnabled ( ) : boolean
return boolean

operator() public method

Create Operator instance to use in update operations
public operator ( ) : Operator
return Operator

readNearest() public method

public readNearest ( array $tags = null )
$tags array

readPrimaryOnly() public method

public readPrimaryOnly ( )

readPrimaryPreferred() public method

public readPrimaryPreferred ( array $tags = null )
$tags array

readSecondaryOnly() public method

public readSecondaryOnly ( array $tags = null )
$tags array

readSecondaryPreferred() public method

public readSecondaryPreferred ( array $tags = null )
$tags array

removeDocumentFromDocumentPool() public method

Remove document instance from identity map
public removeDocumentFromDocumentPool ( Document $document ) : Collection
$document Document
return Collection

setMajorityWriteConcern() public method

Define majority write concern for all requests to current collection
public setMajorityWriteConcern ( integer $timeout = 10000 ) : Collection
$timeout integer timeout in milliseconds
return Collection

setUnacknowledgedWriteConcern() public method

Define unacknowledged write concern for all requests to current collection
public setUnacknowledgedWriteConcern ( integer $timeout = 10000 ) : Collection
$timeout integer timeout in milliseconds
return Collection

setWriteConcern() public method

Define write concern for all requests to current collection
public setWriteConcern ( string | integer $w, integer $timeout = 10000 ) : Collection
$w string | integer write concern
$timeout integer timeout in milliseconds
return Collection

stats() public method

Get collection stat
public stats ( ) : array
return array collection stat

update() public method

Update multiple documents
public update ( Expression | array | callable $expression, Operator | array | callable $updateData, array $options = [] ) : Collection
$expression Expression | array | callable expression to define which documents will change.
$updateData Operator | array | callable new data or operators to update
$options array update options, see http://php.net/manual/ru/mongocollection.update.php
return Collection

updateAll() public method

Update all documents
Deprecation: since 1.13. Use Collection::batchUpdate([])
public updateAll ( Operator | array | callable $updateData ) : Collection
$updateData Operator | array | callable new data or operators
return Collection

updateMultiple() public method

Deprecation: since 1.13 Use Collection::batchUpdate()
public updateMultiple ( $expression, $updateData )

validate() public method

Validates a collection. The method scans a collection’s data structures for correctness and returns a single document that describes the relationship between the logical collection and the physical representation of the data.
public validate ( boolean $full = false ) : array
$full boolean Specify true to enable a full validation and to return full statistics. MongoDB disables full validation by default because it is a potentially resource-intensive operation.
return array

Property Details

$_index protected property

Every index definition must contain key 'keys' with list of fields and orders, and optional options from @link http://php.net/manual/en/mongocollection.createindex.php: Example: array( array( 'keys' => array('field1' => 1, 'field2' => -1), 'unique' => true ), ... )
Deprecation: since 1.13 Use 'index' declaration in mapping
protected $_index

$_queryExpressionClass protected property

expression class. This class may be overloaded to define own query methods (whereUserAgeGreatedThan(), etc.)
Deprecation: since 1.13 Use 'expressionClass' declaration in mapping
protected $_queryExpressionClass

$mongoCollectionClassName protected property

protected $mongoCollectionClassName

$versioning protected property

default value of versioning
Deprecation: since 1.13 Use 'versioning' declaration in mapping
protected $versioning