Moodle PHP Documentation 4.1
Moodle 4.1.11 (Build: 20240610) (c8c84b4af18)
MongoDB\Collection Class Reference

Public Member Functions

 __construct (Manager $manager, string $databaseName, string $collectionName, array $options=[])
 Constructs new Collection instance.
 
 __debugInfo ()
 Return internal properties for debugging purposes.
 
 __toString ()
 Return the collection namespace (e.g.
 
 aggregate (array $pipeline, array $options=[])
 Executes an aggregation framework pipeline on the collection.
 
 bulkWrite (array $operations, array $options=[])
 Executes multiple write operations.
 
 count ($filter=[], array $options=[])
 Gets the number of documents matching the filter.
 
 countDocuments ($filter=[], array $options=[])
 Gets the number of documents matching the filter.
 
 createIndex ($key, array $options=[])
 Create a single index for the collection.
 
 createIndexes (array $indexes, array $options=[])
 Create one or more indexes for the collection.
 
 deleteMany ($filter, array $options=[])
 Deletes all documents matching the filter.
 
 deleteOne ($filter, array $options=[])
 Deletes at most one document matching the filter.
 
 distinct (string $fieldName, $filter=[], array $options=[])
 Finds the distinct values for a specified field across the collection.
 
 drop (array $options=[])
 Drop this collection.
 
 dropIndex ($indexName, array $options=[])
 Drop a single index in the collection.
 
 dropIndexes (array $options=[])
 Drop all indexes in the collection.
 
 estimatedDocumentCount (array $options=[])
 Gets an estimated number of documents in the collection using the collection metadata.
 
 explain (Explainable $explainable, array $options=[])
 Explains explainable commands.
 
 find ($filter=[], array $options=[])
 Finds documents matching the query.
 
 findOne ($filter=[], array $options=[])
 Finds a single document matching the query.
 
 findOneAndDelete ($filter, array $options=[])
 Finds a single document and deletes it, returning the original.
 
 findOneAndReplace ($filter, $replacement, array $options=[])
 Finds a single document and replaces it, returning either the original or the replaced document.
 
 findOneAndUpdate ($filter, $update, array $options=[])
 Finds a single document and updates it, returning either the original or the updated document.
 
 getCollectionName ()
 Return the collection name.
 
 getDatabaseName ()
 Return the database name.
 
 getManager ()
 Return the Manager.
 
 getNamespace ()
 Return the collection namespace.
 
 getReadConcern ()
 Return the read concern for this collection.
 
 getReadPreference ()
 Return the read preference for this collection.
 
 getTypeMap ()
 Return the type map for this collection.
 
 getWriteConcern ()
 Return the write concern for this collection.
 
 insertMany (array $documents, array $options=[])
 Inserts multiple documents.
 
 insertOne ($document, array $options=[])
 Inserts one document.
 
 listIndexes (array $options=[])
 Returns information for all indexes for the collection.
 
 mapReduce (JavascriptInterface $map, JavascriptInterface $reduce, $out, array $options=[])
 Executes a map-reduce aggregation on the collection.
 
 rename (string $toCollectionName, ?string $toDatabaseName=null, array $options=[])
 Renames the collection.
 
 replaceOne ($filter, $replacement, array $options=[])
 Replaces at most one document matching the filter.
 
 updateMany ($filter, $update, array $options=[])
 Updates all documents matching the filter.
 
 updateOne ($filter, $update, array $options=[])
 Updates at most one document matching the filter.
 
 watch (array $pipeline=[], array $options=[])
 Create a change stream for watching changes to the collection.
 
 withOptions (array $options=[])
 Get a clone of this collection with different options.
 

Constructor & Destructor Documentation

◆ __construct()

MongoDB\Collection::__construct ( Manager $manager,
string $databaseName,
string $collectionName,
array $options = [] )

Constructs new Collection instance.

This class provides methods for collection-specific operations, such as CRUD (i.e. create, read, update, and delete) and index management.

Supported options:

  • readConcern (MongoDB\Driver\ReadConcern): The default read concern to use for collection operations. Defaults to the Manager's read concern.
  • readPreference (MongoDB\Driver\ReadPreference): The default read preference to use for collection operations. Defaults to the Manager's read preference.
  • typeMap (array): Default type map for cursors and BSON documents.
  • writeConcern (MongoDB\Driver\WriteConcern): The default write concern to use for collection operations. Defaults to the Manager's write concern.
Parameters
Manager$managerManager instance from the driver
string$databaseNameDatabase name
string$collectionNameCollection name
array$optionsCollection options
Exceptions
InvalidArgumentExceptionfor parameter/option parsing errors

Member Function Documentation

◆ __debugInfo()

MongoDB\Collection::__debugInfo ( )

Return internal properties for debugging purposes.

See also
https://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
Return values
array

◆ __toString()

MongoDB\Collection::__toString ( )

Return the collection namespace (e.g.

"db.collection").

See also
https://mongodb.com/docs/manual/core/databases-and-collections/
Return values
string

◆ aggregate()

MongoDB\Collection::aggregate ( array $pipeline,
array $options = [] )

Executes an aggregation framework pipeline on the collection.

Note: this method's return value depends on the MongoDB server version and the "useCursor" option. If "useCursor" is true, a Cursor will be returned; otherwise, an ArrayIterator is returned, which wraps the "result" array from the command response document.

See also
Aggregate\__construct() for supported options
Parameters
array$pipelineList of pipeline operations
array$optionsCommand options
Return values
Traversable
Exceptions
UnexpectedValueExceptionif the command response was malformed
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ bulkWrite()

MongoDB\Collection::bulkWrite ( array $operations,
array $options = [] )

Executes multiple write operations.

See also
BulkWrite\__construct() for supported options
Parameters
array[]$operationsList of write operations
array$optionsCommand options
Return values
BulkWriteResult
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ count()

MongoDB\Collection::count ( $filter = [],
array $options = [] )

Gets the number of documents matching the filter.

See also
Count\__construct() for supported options
Parameters
array | object$filterQuery by which to filter documents
array$optionsCommand options
Return values
integer
Exceptions
UnexpectedValueExceptionif the command response was malformed
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)
Deprecated
1.4

◆ countDocuments()

MongoDB\Collection::countDocuments ( $filter = [],
array $options = [] )

Gets the number of documents matching the filter.

See also
CountDocuments\__construct() for supported options
Parameters
array | object$filterQuery by which to filter documents
array$optionsCommand options
Return values
integer
Exceptions
UnexpectedValueExceptionif the command response was malformed
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ createIndex()

MongoDB\Collection::createIndex ( $key,
array $options = [] )

Create a single index for the collection.

See also
Collection\createIndexes()
CreateIndexes\__construct() for supported command options
Parameters
array | object$keyDocument containing fields mapped to values, which denote order or an index type
array$optionsIndex and command options
Return values
stringThe name of the created index
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ createIndexes()

MongoDB\Collection::createIndexes ( array $indexes,
array $options = [] )

Create one or more indexes for the collection.

Each element in the $indexes array must have a "key" document, which contains fields mapped to an order or type. Other options may follow. For example:

$indexes = [
    // Create a unique index on the "username" field
    [ 'key' => [ 'username' => 1 ], 'unique' => true ],
    // Create a 2dsphere index on the "loc" field with a custom name
    [ 'key' => [ 'loc' => '2dsphere' ], 'name' => 'geo' ],
];

If the "name" option is unspecified, a name will be generated from the "key" document.

See also
https://mongodb.com/docs/manual/reference/command/createIndexes/
https://mongodb.com/docs/manual/reference/method/db.collection.createIndex/
CreateIndexes\__construct() for supported command options
Parameters
array[]$indexesList of index specifications
array$optionsCommand options
Return values
string[]The names of the created indexes
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ deleteMany()

MongoDB\Collection::deleteMany ( $filter,
array $options = [] )

Deletes all documents matching the filter.

See also
DeleteMany\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/delete/
Parameters
array | object$filterQuery by which to delete documents
array$optionsCommand options
Return values
DeleteResult
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ deleteOne()

MongoDB\Collection::deleteOne ( $filter,
array $options = [] )

Deletes at most one document matching the filter.

See also
DeleteOne\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/delete/
Parameters
array | object$filterQuery by which to delete documents
array$optionsCommand options
Return values
DeleteResult
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ distinct()

MongoDB\Collection::distinct ( string $fieldName,
$filter = [],
array $options = [] )

Finds the distinct values for a specified field across the collection.

See also
Distinct\__construct() for supported options
Parameters
string$fieldNameField for which to return distinct values
array | object$filterQuery by which to filter documents
array$optionsCommand options
Return values
array
Exceptions
UnexpectedValueExceptionif the command response was malformed
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ drop()

MongoDB\Collection::drop ( array $options = [])

Drop this collection.

See also
DropCollection\__construct() for supported options
Parameters
array$optionsAdditional options
Return values
array|objectCommand result document
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ dropIndex()

MongoDB\Collection::dropIndex ( $indexName,
array $options = [] )

Drop a single index in the collection.

See also
DropIndexes\__construct() for supported options
Parameters
string | IndexInfo$indexNameIndex name or model object
array$optionsAdditional options
Return values
array|objectCommand result document
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ dropIndexes()

MongoDB\Collection::dropIndexes ( array $options = [])

Drop all indexes in the collection.

See also
DropIndexes\__construct() for supported options
Parameters
array$optionsAdditional options
Return values
array|objectCommand result document
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ estimatedDocumentCount()

MongoDB\Collection::estimatedDocumentCount ( array $options = [])

Gets an estimated number of documents in the collection using the collection metadata.

See also
EstimatedDocumentCount\__construct() for supported options
Parameters
array$optionsCommand options
Return values
integer
Exceptions
UnexpectedValueExceptionif the command response was malformed
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ explain()

MongoDB\Collection::explain ( Explainable $explainable,
array $options = [] )

Explains explainable commands.

See also
Explain\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/explain/
Parameters
Explainable$explainableCommand on which to run explain
array$optionsAdditional options
Return values
array|object
Exceptions
UnsupportedExceptionif explainable or options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ find()

MongoDB\Collection::find ( $filter = [],
array $options = [] )

Finds documents matching the query.

See also
Find\__construct() for supported options
https://mongodb.com/docs/manual/crud/#read-operations
Parameters
array | object$filterQuery by which to filter documents
array$optionsAdditional options
Return values
Cursor
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ findOne()

MongoDB\Collection::findOne ( $filter = [],
array $options = [] )

Finds a single document matching the query.

See also
FindOne\__construct() for supported options
https://mongodb.com/docs/manual/crud/#read-operations
Parameters
array | object$filterQuery by which to filter documents
array$optionsAdditional options
Return values
array|object|null
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ findOneAndDelete()

MongoDB\Collection::findOneAndDelete ( $filter,
array $options = [] )

Finds a single document and deletes it, returning the original.

The document to return may be null if no document matched the filter.

See also
FindOneAndDelete\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/findAndModify/
Parameters
array | object$filterQuery by which to filter documents
array$optionsCommand options
Return values
array|object|null
Exceptions
UnexpectedValueExceptionif the command response was malformed
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ findOneAndReplace()

MongoDB\Collection::findOneAndReplace ( $filter,
$replacement,
array $options = [] )

Finds a single document and replaces it, returning either the original or the replaced document.

The document to return may be null if no document matched the filter. By default, the original document is returned. Specify FindOneAndReplace\RETURN_DOCUMENT_AFTER for the "returnDocument" option to return the updated document.

See also
FindOneAndReplace\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/findAndModify/
Parameters
array | object$filterQuery by which to filter documents
array | object$replacementReplacement document
array$optionsCommand options
Return values
array|object|null
Exceptions
UnexpectedValueExceptionif the command response was malformed
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ findOneAndUpdate()

MongoDB\Collection::findOneAndUpdate ( $filter,
$update,
array $options = [] )

Finds a single document and updates it, returning either the original or the updated document.

The document to return may be null if no document matched the filter. By default, the original document is returned. Specify FindOneAndUpdate\RETURN_DOCUMENT_AFTER for the "returnDocument" option to return the updated document.

See also
FindOneAndReplace\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/findAndModify/
Parameters
array | object$filterQuery by which to filter documents
array | object$updateUpdate to apply to the matched document
array$optionsCommand options
Return values
array|object|null
Exceptions
UnexpectedValueExceptionif the command response was malformed
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ getCollectionName()

MongoDB\Collection::getCollectionName ( )

Return the collection name.

Return values
string

◆ getDatabaseName()

MongoDB\Collection::getDatabaseName ( )

Return the database name.

Return values
string

◆ getManager()

MongoDB\Collection::getManager ( )

Return the Manager.

Return values
Manager

◆ getNamespace()

MongoDB\Collection::getNamespace ( )

Return the collection namespace.

See also
https://mongodb.com/docs/manual/reference/glossary/#term-namespace
Return values
string

◆ getReadConcern()

MongoDB\Collection::getReadConcern ( )

Return the read concern for this collection.

See also
https://php.net/manual/en/mongodb-driver-readconcern.isdefault.php
Return values
ReadConcern

◆ getReadPreference()

MongoDB\Collection::getReadPreference ( )

Return the read preference for this collection.

Return values
ReadPreference

◆ getTypeMap()

MongoDB\Collection::getTypeMap ( )

Return the type map for this collection.

Return values
array

◆ getWriteConcern()

MongoDB\Collection::getWriteConcern ( )

Return the write concern for this collection.

See also
https://php.net/manual/en/mongodb-driver-writeconcern.isdefault.php
Return values
WriteConcern

◆ insertMany()

MongoDB\Collection::insertMany ( array $documents,
array $options = [] )

Inserts multiple documents.

See also
InsertMany\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/insert/
Parameters
array[] | object[]$documentsThe documents to insert
array$optionsCommand options
Return values
InsertManyResult
Exceptions
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ insertOne()

MongoDB\Collection::insertOne ( $document,
array $options = [] )

Inserts one document.

See also
InsertOne\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/insert/
Parameters
array | object$documentThe document to insert
array$optionsCommand options
Return values
InsertOneResult
Exceptions
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ listIndexes()

MongoDB\Collection::listIndexes ( array $options = [])

Returns information for all indexes for the collection.

See also
ListIndexes\__construct() for supported options
Return values
IndexInfoIterator
Exceptions
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ mapReduce()

MongoDB\Collection::mapReduce ( JavascriptInterface $map,
JavascriptInterface $reduce,
$out,
array $options = [] )

Executes a map-reduce aggregation on the collection.

See also
MapReduce\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/mapReduce/
Parameters
JavascriptInterface$mapMap function
JavascriptInterface$reduceReduce function
string | array | object$outOutput specification
array$optionsCommand options
Return values
MapReduceResult
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)
UnexpectedValueExceptionif the command response was malformed

◆ rename()

MongoDB\Collection::rename ( string $toCollectionName,
?string $toDatabaseName = null,
array $options = [] )

Renames the collection.

See also
RenameCollection\__construct() for supported options
Parameters
string$toCollectionNameNew name of the collection
string | null$toDatabaseNameNew database name of the collection. Defaults to the original database.
array$optionsAdditional options
Return values
array|objectCommand result document
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ replaceOne()

MongoDB\Collection::replaceOne ( $filter,
$replacement,
array $options = [] )

Replaces at most one document matching the filter.

See also
ReplaceOne\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/update/
Parameters
array | object$filterQuery by which to filter documents
array | object$replacementReplacement document
array$optionsCommand options
Return values
UpdateResult
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ updateMany()

MongoDB\Collection::updateMany ( $filter,
$update,
array $options = [] )

Updates all documents matching the filter.

See also
UpdateMany\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/update/
Parameters
array | object$filterQuery by which to filter documents
array | object$updateUpdate to apply to the matched documents
array$optionsCommand options
Return values
UpdateResult
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ updateOne()

MongoDB\Collection::updateOne ( $filter,
$update,
array $options = [] )

Updates at most one document matching the filter.

See also
UpdateOne\__construct() for supported options
https://mongodb.com/docs/manual/reference/command/update/
Parameters
array | object$filterQuery by which to filter documents
array | object$updateUpdate to apply to the matched document
array$optionsCommand options
Return values
UpdateResult
Exceptions
UnsupportedExceptionif options are not supported by the selected server
InvalidArgumentExceptionfor parameter/option parsing errors
DriverRuntimeExceptionfor other driver errors (e.g. connection errors)

◆ watch()

MongoDB\Collection::watch ( array $pipeline = [],
array $options = [] )

Create a change stream for watching changes to the collection.

See also
Watch\__construct() for supported options
Parameters
array$pipelineList of pipeline operations
array$optionsCommand options
Return values
ChangeStream
Exceptions
InvalidArgumentExceptionfor parameter/option parsing errors

◆ withOptions()

MongoDB\Collection::withOptions ( array $options = [])

Get a clone of this collection with different options.

See also
Collection\__construct() for supported options
Parameters
array$optionsCollection constructor options
Return values
Collection
Exceptions
InvalidArgumentExceptionfor parameter/option parsing errors

The documentation for this class was generated from the following file: