class GridFS extends Collection (View source)

Wrapper for the MongoGridFS class.

This class does not proxy all of the MongoGridFS methods; however, the MongoGridFS object is accessible if those methods are required.

Properties

protected Database $database The Database instance to which this collection belongs. from Collection
protected EventManager $eventManager The EventManager used to dispatch events. from Collection
protected MongoGridFS $mongoCollection The MongoGridFS instance being wrapped.
protected integer $numRetries Number of times to retry queries. from Collection

Methods

__construct(Database $database, MongoGridFS $mongoGridFS, EventManager $evm, integer $numRetries = 0)

Constructor.

aggregate(array $pipeline, array $options = [])

Invokes the aggregate command.

array|boolean
batchInsert(array $a, array $options = [])

Wrapper method for MongoCollection::batchInsert().

integer
count(array $query = [], integer|array $limitOrOptions = 0, integer $skip = 0)

Invokes the count command.

createAggregationBuilder()

Creates a new aggregation builder instance.

array
createDBRef(mixed $documentOrId)

Wrapper method for MongoCollection::createDBRef().

createQueryBuilder()

Creates a new query builder instance.

array
deleteIndex(array|string $keys)

Wrapper method for MongoCollection::deleteIndex().

array
deleteIndexes()

Wrapper method for MongoCollection::deleteIndexes().

distinct(string $field, array $query = [], array $options = [])

Invokes the distinct command.

array
drop()

Wrapper method for MongoCollection::drop().

array|boolean
ensureIndex(array $keys, array $options = [])

Wrapper method for MongoCollection::ensureIndex().

find(array $query = [], array $fields = [])

Wrapper method for MongoCollection::find().

array|null
findAndRemove(array $query, array $options = [])

Invokes the findAndModify command with the remove option.

array|null
findAndUpdate(array $query, array $newObj, array $options = [])

Invokes the findAndModify command with the update option.

array|null
findOne(array $query = [], array $fields = [])

Wrapper method for MongoCollection::findOne().

getDatabase()

Return the database for this collection.

array|null
getDBRef(array $reference)

Wrapper method for MongoCollection::getDBRef().

array
getIndexInfo()

Wrapper method for MongoCollection::getIndexInfo().

MongoCollection
getMongoCollection()

Return the MongoGridFS instance being wrapped.

string
getName()

Wrapper method for MongoCollection::getName().

array
getReadPreference()

Wrapper method for MongoCollection::getReadPreference().

boolean
setReadPreference(string $readPreference, array $tags = null)

Wrapper method for MongoCollection::setReadPreference().

boolean
getSlaveOkay()

Get whether secondary read queries are allowed for this collection.

boolean
setSlaveOkay(boolean $ok = true)

Set whether secondary read queries are allowed for this collection.

group(array|string|MongoCode $keys, array $initial, string|MongoCode $reduce, array $options = [])

Invokes the group command.

array|boolean
insert(array $a, array $options = [])

Wrapper method for MongoCollection::insert().

boolean
isFieldIndexed(string $fieldName)

Check if a given field name is indexed in MongoDB.

mapReduce(string|MongoCode $map, string|MongoCode $reduce, array|string $out = ['inline' => true], array $query = [], array $options = [])

Invokes the mapReduce command.

near(array|Point $near, array $query = [], array $options = [])

Invokes the geoNear command.

parallelCollectionScan(int $numCursors)

Wrapper method for MongoCollection::parallelCollectionScan()

array|boolean
remove(array $query, array $options = [])

Wrapper method for MongoCollection::remove().

array|boolean
save(array $a, array $options = [])

Wrapper method for MongoCollection::save().

array|boolean
update(array $query, array $newObj, array $options = [])

Wrapper method for MongoCollection::update().

array|boolean
upsert(array $query, array $newObj, array $options = [])

Invokes {@link Collection::update()} with the upsert option.

array
validate(string $scanData = false)

Wrapper method for MongoCollection::validate().

MongoCollection
__get(string $name)

Wrapper method for MongoCollection::__get().

string
__toString()

Wrapper method for MongoCollection::__toString().

doAggregate(array $pipeline, array $options = [])

Execute the aggregate command.

doAggregateCursor(array $pipeline, array $options = [])

Executes the aggregate command and returns a MongoCommandCursor.

array|boolean
doBatchInsert(array $a, array $options = [])

Execute the batchInsert query.

integer
doCount(array $query, array $options)

Execute the count command.

doDistinct(string $field, array $query, array $options)

Execute the distinct command.

array
doDrop()

Drops the collection.

doFind(array $query, array $fields)

Execute the find query.

array|null
doFindAndRemove(array $query, array $options = [])

Execute the findAndModify command with the remove option and delete any chunks for the document.

array|null
doFindAndUpdate(array $query, array $newObj, array $options)

Execute the findAndModify command with the update option.

array|null
doFindOne(array $query = [], array $fields = [])

Execute the findOne query.

array|null
doGetDBRef(array $reference)

Resolves a database reference.

doGroup(array|string|MongoCode $keys, array $initial, string|MongoCode $reduce, array $options)

Execute the group command.

array|boolean
doInsert(array $a, array $options = [])

Execute the insert query and persist the GridFSFile if necessary.

doMapReduce(string|MongoCode $map, string|MongoCode $reduce, array|string $out, array $query, array $options)

Execute the mapReduce command.

doNear(array|Point $near, array $query, array $options)

Execute the geoNear command.

array|boolean
doRemove(array $query, array $options)

Execute the remove query.

array|boolean
doSave(array $a, array $options = [])

Execute the save query and persist the GridFSFile if necessary.

array|boolean
doUpdate(array $query, array $newObj, array $options = [])

Execute the update query and persist its GridFSFile if necessary.

mixed
retry(Closure $retry)

Conditionally retry a closure if it yields an exception.

wrapCommandCursor(MongoCommandCursor $commandCursor)

Wraps a MongoCommandCursor instance with a CommandCursor.

wrapCursor(MongoCursor $cursor, array $query, array $fields)

Wraps a MongoCursor instance with a Cursor.

array
convertWriteConcern(array $options)

Converts "safe" write option to "w" for driver versions 1.3.0+.

array
convertWriteTimeout(array $options)

Convert "wtimeout" write option to "wTimeoutMS" for driver version 1.5.0+.

array
convertSocketTimeout(array $options)

Convert "timeout" write option to "socketTimeoutMS" for driver version 1.5.0+.

array
splitCommandAndClientOptions(array $options)

Splits a command helper's options array into command and client options.

storeFile(string|GridFSFile $file, array $document, array $options = [])

Wrapper method for MongoGridFS::storeFile().

Details

__construct(Database $database, MongoGridFS $mongoGridFS, EventManager $evm, integer $numRetries = 0)

Constructor.

Parameters

Database $database Database to which this collection belongs
MongoGridFS $mongoGridFS MongoGridFS instance being wrapped
EventManager $evm EventManager instance
integer $numRetries Number of times to retry queries

Iterator aggregate(array $pipeline, array $options = [])

Invokes the aggregate command.

This method will dispatch preAggregate and postAggregate events.

By default, the results from a non-cursor aggregate command will be returned as an ArrayIterator; however, if the pipeline ends in an $out operator, a cursor on the output collection will be returned instead.

If the "cursor" option is true or an array, a command cursor will be returned (requires driver >= 1.5.0 and MongoDB >= 2.6).

Parameters

array $pipeline
array $options

Return Value

Iterator

Exceptions

ResultException if the command fails

See also

http://php.net/manual/en/mongocollection.aggregate.php
http://docs.mongodb.org/manual/reference/command/aggregate/

array|boolean batchInsert(array $a, array $options = [])

Wrapper method for MongoCollection::batchInsert().

This method will dispatch preBatchInsert and postBatchInsert events.

Parameters

array $a Array of documents (arrays/objects) to insert
array $options

Return Value

array|boolean

See also

http://php.net/manual/en/mongocollection.batchinsert.php

integer count(array $query = [], integer|array $limitOrOptions = 0, integer $skip = 0)

Invokes the count command.

Parameters

array $query
integer|array $limitOrOptions Limit or options array
integer $skip

Return Value

integer

See also

http://php.net/manual/en/mongocollection.count.php
http://docs.mongodb.org/manual/reference/command/count/

Builder createAggregationBuilder()

Creates a new aggregation builder instance.

Return Value

Builder

array createDBRef(mixed $documentOrId)

Wrapper method for MongoCollection::createDBRef().

Parameters

mixed $documentOrId

Return Value

array

See also

http://php.net/manual/en/mongocollection.createdbref.php

Builder createQueryBuilder()

Creates a new query builder instance.

Return Value

Builder

array deleteIndex(array|string $keys)

Wrapper method for MongoCollection::deleteIndex().

Parameters

array|string $keys

Return Value

array

See also

http://php.net/manual/en/mongocollection.deleteindex.php

array deleteIndexes()

Wrapper method for MongoCollection::deleteIndexes().

ArrayIterator distinct(string $field, array $query = [], array $options = [])

Invokes the distinct command.

This method will dispatch preDistinct and postDistinct events.

Parameters

string $field
array $query
array $options

Return Value

ArrayIterator

Exceptions

ResultException if the command fails

See also

http://php.net/manual/en/mongocollection.distinct.php
http://docs.mongodb.org/manual/reference/command/distinct/

array drop()

Wrapper method for MongoCollection::drop().

This method will dispatch preDropCollection and postDropCollection events.

array|boolean ensureIndex(array $keys, array $options = [])

Wrapper method for MongoCollection::ensureIndex().

Parameters

array $keys
array $options

Return Value

array|boolean

See also

http://php.net/manual/en/mongocollection.ensureindex.php

Cursor find(array $query = [], array $fields = [])

Wrapper method for MongoCollection::find().

This method will dispatch preFind and postFind events.

Parameters

array $query
array $fields

Return Value

Cursor

See also

http://php.net/manual/en/mongocollection.find.php

array|null findAndRemove(array $query, array $options = [])

Invokes the findAndModify command with the remove option.

This method will dispatch preFindAndRemove and postFindAndRemove events.

Parameters

array $query
array $options

Return Value

array|null

Exceptions

ResultException if the command fails

See also

http://docs.mongodb.org/manual/reference/command/findAndModify/

array|null findAndUpdate(array $query, array $newObj, array $options = [])

Invokes the findAndModify command with the update option.

This method will dispatch preFindAndUpdate and postFindAndUpdate events.

Parameters

array $query
array $newObj
array $options

Return Value

array|null

Exceptions

ResultException if the command fails

See also

http://docs.mongodb.org/manual/reference/command/findAndModify/

array|null findOne(array $query = [], array $fields = [])

Wrapper method for MongoCollection::findOne().

This method will dispatch preFindOne and postFindOne events.

Parameters

array $query
array $fields

Return Value

array|null

See also

http://php.net/manual/en/mongocollection.findone.php

Database getDatabase()

Return the database for this collection.

Return Value

Database

array|null getDBRef(array $reference)

Wrapper method for MongoCollection::getDBRef().

This method will dispatch preGetDBRef and postGetDBRef events.

Parameters

array $reference

Return Value

array|null

See also

http://php.net/manual/en/mongocollection.getdbref.php

array getIndexInfo()

Wrapper method for MongoCollection::getIndexInfo().

MongoCollection getMongoCollection()

Return the MongoGridFS instance being wrapped.

Return Value

MongoCollection

See also

Collection::getMongoCollection

string getName()

Wrapper method for MongoCollection::getName().

array getReadPreference()

Wrapper method for MongoCollection::getReadPreference().

For driver versions between 1.3.0 and 1.3.3, the return value will be converted for consistency with {@link Collection::setReadPreference()}.

boolean setReadPreference(string $readPreference, array $tags = null)

Wrapper method for MongoCollection::setReadPreference().

Parameters

string $readPreference
array $tags

Return Value

boolean

See also

http://php.net/manual/en/mongocollection.setreadpreference.php

boolean getSlaveOkay()

Get whether secondary read queries are allowed for this collection.

This method wraps getSlaveOkay() for driver versions before 1.3.0. For newer drivers, this method considers any read preference other than PRIMARY as a true "slaveOkay" value.

boolean setSlaveOkay(boolean $ok = true)

Set whether secondary read queries are allowed for this collection.

This method wraps setSlaveOkay() for driver versions before 1.3.0. For newer drivers, this method wraps setReadPreference() and specifies SECONDARY_PREFERRED.

ArrayIterator group(array|string|MongoCode $keys, array $initial, string|MongoCode $reduce, array $options = [])

Invokes the group command.

This method will dispatch preGroup and postGroup events.

Parameters

array|string|MongoCode $keys
array $initial
string|MongoCode $reduce
array $options

Return Value

ArrayIterator

Exceptions

ResultException if the command fails

See also

http://www.php.net/manual/en/mongocollection.group.php
http://docs.mongodb.org/manual/reference/command/group/

array|boolean insert(array $a, array $options = [])

Wrapper method for MongoCollection::insert().

This method will dispatch preInsert and postInsert events.

Parameters

array $a Document to insert
array $options

Return Value

array|boolean

See also

http://php.net/manual/en/mongocollection.insert.php

boolean isFieldIndexed(string $fieldName)

Check if a given field name is indexed in MongoDB.

Parameters

string $fieldName

Return Value

boolean

ArrayIterator|Cursor mapReduce(string|MongoCode $map, string|MongoCode $reduce, array|string $out = ['inline' => true], array $query = [], array $options = [])

Invokes the mapReduce command.

This method will dispatch preMapReduce and postMapReduce events.

If the output method is inline, an ArrayIterator will be returned. Otherwise, a Cursor to all documents in the output collection will be returned.

Parameters

string|MongoCode $map
string|MongoCode $reduce
array|string $out
array $query
array $options

Return Value

ArrayIterator|Cursor

Exceptions

ResultException if the command fails

See also

http://docs.mongodb.org/manual/reference/command/mapReduce/

ArrayIterator near(array|Point $near, array $query = [], array $options = [])

Invokes the geoNear command.

This method will dispatch preNear and postNear events.

The $near parameter may be a GeoJSON point or a legacy coordinate pair, which is an array of float values in x, y order (easting, northing for projected coordinates, longitude, latitude for geographic coordinates). A GeoJSON point may be a Point object or an array corresponding to the point's JSON representation.

Parameters

array|Point $near
array $query
array $options

Return Value

ArrayIterator

Exceptions

ResultException if the command fails

See also

http://docs.mongodb.org/manual/reference/command/geoNear/

CommandCursor[] parallelCollectionScan(int $numCursors)

Wrapper method for MongoCollection::parallelCollectionScan()

Parameters

int $numCursors

Return Value

CommandCursor[]

Exceptions

BadMethodCallException if MongoCollection::parallelCollectionScan() is not available

array|boolean remove(array $query, array $options = [])

Wrapper method for MongoCollection::remove().

This method will dispatch preRemove and postRemove events.

Parameters

array $query
array $options

Return Value

array|boolean

See also

http://php.net/manual/en/mongocollection.remove.php

array|boolean save(array $a, array $options = [])

Wrapper method for MongoCollection::save().

This method will dispatch preSave and postSave events.

Parameters

array $a Document to save
array $options

Return Value

array|boolean

See also

http://php.net/manual/en/mongocollection.remove.php

array|boolean update(array $query, array $newObj, array $options = [])

Wrapper method for MongoCollection::update().

This method will dispatch preUpdate and postUpdate events.

Parameters

array $query
array $newObj
array $options

Return Value

array|boolean

See also

http://php.net/manual/en/mongocollection.update.php

array|boolean upsert(array $query, array $newObj, array $options = [])

Invokes {@link Collection::update()} with the upsert option.

This method will dispatch preUpdate and postUpdate events.

Parameters

array $query
array $newObj
array $options

Return Value

array|boolean

See also

Collection::update
http://php.net/manual/en/mongocollection.update.php

array validate(string $scanData = false)

Wrapper method for MongoCollection::validate().

Parameters

string $scanData

Return Value

array

See also

http://php.net/manual/en/mongocollection.validate.php

MongoCollection __get(string $name)

Wrapper method for MongoCollection::__get().

Parameters

string $name

Return Value

MongoCollection

See also

http://php.net/manual/en/mongocollection.get.php

string __toString()

Wrapper method for MongoCollection::__toString().

protected Iterator doAggregate(array $pipeline, array $options = [])

Execute the aggregate command.

Parameters

array $pipeline
array $options

Return Value

Iterator

See also

Collection::aggregate

protected CommandCursor doAggregateCursor(array $pipeline, array $options = [])

Executes the aggregate command and returns a MongoCommandCursor.

Parameters

array $pipeline
array $options

Return Value

CommandCursor

Exceptions

BadMethodCallException if MongoCollection::aggregateCursor() is not available

protected array|boolean doBatchInsert(array $a, array $options = [])

Execute the batchInsert query.

Parameters

array $a
array $options

Return Value

array|boolean

See also

Collection::doBatchInsert

protected integer doCount(array $query, array $options)

Execute the count command.

Parameters

array $query
array $options

Return Value

integer

Exceptions

ResultException if the command fails or omits the result field

See also

Collection::count

protected ArrayIterator doDistinct(string $field, array $query, array $options)

Execute the distinct command.

Parameters

string $field
array $query
array $options

Return Value

ArrayIterator

Exceptions

ResultException if the command fails

See also

Collection::distinct

protected array doDrop()

Drops the collection.

Return Value

array

See also

Collection::drop

protected Cursor doFind(array $query, array $fields)

Execute the find query.

Parameters

array $query
array $fields

Return Value

Cursor

See also

Collection::find

protected array|null doFindAndRemove(array $query, array $options = [])

Execute the findAndModify command with the remove option and delete any chunks for the document.

Parameters

array $query
array $options

Return Value

array|null

See also

Collection::doFindAndRemove

protected array|null doFindAndUpdate(array $query, array $newObj, array $options)

Execute the findAndModify command with the update option.

Parameters

array $query
array $newObj
array $options

Return Value

array|null

Exceptions

ResultException if the command fails

See also

Collection::findAndUpdate

protected array|null doFindOne(array $query = [], array $fields = [])

Execute the findOne query.

This method returns the file document, unlike the base MongoGridFS method, which returns a MongoGridFSFile instance. Instead, the document's "file" field will contain an equivalent GridFSFile instance.

Parameters

array $query
array $fields

Return Value

array|null

See also

Collection::doFindOne

protected array|null doGetDBRef(array $reference)

Resolves a database reference.

Parameters

array $reference

Return Value

array|null

See also

Collection::getDBRef

protected ArrayIterator doGroup(array|string|MongoCode $keys, array $initial, string|MongoCode $reduce, array $options)

Execute the group command.

Parameters

array|string|MongoCode $keys
array $initial
string|MongoCode $reduce
array $options

Return Value

ArrayIterator

Exceptions

ResultException if the command fails

See also

Collection::group

protected array|boolean doInsert(array $a, array $options = [])

Execute the insert query and persist the GridFSFile if necessary.

Parameters

array $a
array $options

Return Value

array|boolean

See also

Collection::doInsert

protected ArrayIterator doMapReduce(string|MongoCode $map, string|MongoCode $reduce, array|string $out, array $query, array $options)

Execute the mapReduce command.

Parameters

string|MongoCode $map
string|MongoCode $reduce
array|string $out
array $query
array $options

Return Value

ArrayIterator

Exceptions

ResultException if the command fails

See also

Collection::mapReduce

protected ArrayIterator doNear(array|Point $near, array $query, array $options)

Execute the geoNear command.

Parameters

array|Point $near
array $query
array $options

Return Value

ArrayIterator

Exceptions

ResultException if the command fails

See also

Collection::near

protected array|boolean doRemove(array $query, array $options)

Execute the remove query.

Parameters

array $query
array $options

Return Value

array|boolean

See also

Collection::remove

protected array|boolean doSave(array $a, array $options = [])

Execute the save query and persist the GridFSFile if necessary.

Parameters

array $a
array $options

Return Value

array|boolean

See also

Collection::doSave

protected array|boolean doUpdate(array $query, array $newObj, array $options = [])

Execute the update query and persist its GridFSFile if necessary.

Parameters

array $query
array $newObj
array $options

Return Value

array|boolean

See also

Collection::doFindOne

protected mixed retry(Closure $retry)

Conditionally retry a closure if it yields an exception.

If the closure does not return successfully within the configured number of retries, its first exception will be thrown.

This method should not be used for write operations.

Parameters

Closure $retry

Return Value

mixed

protected CommandCursor wrapCommandCursor(MongoCommandCursor $commandCursor)

Wraps a MongoCommandCursor instance with a CommandCursor.

Parameters

MongoCommandCursor $commandCursor

Return Value

CommandCursor

protected Cursor wrapCursor(MongoCursor $cursor, array $query, array $fields)

Wraps a MongoCursor instance with a Cursor.

Parameters

MongoCursor $cursor
array $query
array $fields

Return Value

Cursor

protected array convertWriteConcern(array $options)

Converts "safe" write option to "w" for driver versions 1.3.0+.

Parameters

array $options

Return Value

array

protected array convertWriteTimeout(array $options)

Convert "wtimeout" write option to "wTimeoutMS" for driver version 1.5.0+.

Parameters

array $options

Return Value

array

protected array convertSocketTimeout(array $options)

Convert "timeout" write option to "socketTimeoutMS" for driver version 1.5.0+.

Parameters

array $options

Return Value

array

protected array splitCommandAndClientOptions(array $options)

Splits a command helper's options array into command and client options.

Command options are intended to be merged into the command document. Client options (e.g. socket timeout) are for {@link Database::command()}.

Parameters

array $options

Return Value

array Tuple of command options and client options

GridFSFile storeFile(string|GridFSFile $file, array $document, array $options = [])

Wrapper method for MongoGridFS::storeFile().

This method returns the GridFSFile object, unlike the base MongoGridFS method, which returns the "_id" field of the saved document. The "_id" will be set on the $document parameter, which is passed by reference.

Parameters

string|GridFSFile $file String filename or a GridFSFile object
array $document
array $options

Return Value

GridFSFile

See also

http://php.net/manual/en/mongogridfs.storefile.php