Documentation

Collection
in package

Table of Contents

$collectionName  : string
$databaseName  : string
$defaultTypeMap  : array<string|int, mixed>
$manager  : Manager
$readConcern  : ReadConcern
$readPreference  : ReadPreference
$typeMap  : array<string|int, mixed>
$wireVersionForReadConcernWithWriteStage  : int
$writeConcern  : WriteConcern
__construct()  : mixed
Constructs new Collection instance.
__debugInfo()  : array<string|int, mixed>
Return internal properties for debugging purposes.
__toString()  : string
Return the collection namespace (e.g. "db.collection").
aggregate()  : Traversable
Executes an aggregation framework pipeline on the collection.
bulkWrite()  : BulkWriteResult
Executes multiple write operations.
count()  : int
Gets the number of documents matching the filter.
countDocuments()  : int
Gets the number of documents matching the filter.
createIndex()  : string
Create a single index for the collection.
createIndexes()  : array<string|int, string>
Create one or more indexes for the collection.
deleteMany()  : DeleteResult
Deletes all documents matching the filter.
deleteOne()  : DeleteResult
Deletes at most one document matching the filter.
distinct()  : array<string|int, mixed>
Finds the distinct values for a specified field across the collection.
drop()  : array<string|int, mixed>|object
Drop this collection.
dropIndex()  : array<string|int, mixed>|object
Drop a single index in the collection.
dropIndexes()  : array<string|int, mixed>|object
Drop all indexes in the collection.
estimatedDocumentCount()  : int
Gets an estimated number of documents in the collection using the collection metadata.
explain()  : array<string|int, mixed>|object
Explains explainable commands.
find()  : Cursor
Finds documents matching the query.
findOne()  : array<string|int, mixed>|object|null
Finds a single document matching the query.
findOneAndDelete()  : array<string|int, mixed>|object|null
Finds a single document and deletes it, returning the original.
findOneAndReplace()  : array<string|int, mixed>|object|null
Finds a single document and replaces it, returning either the original or the replaced document.
findOneAndUpdate()  : array<string|int, mixed>|object|null
Finds a single document and updates it, returning either the original or the updated document.
getCollectionName()  : string
Return the collection name.
getDatabaseName()  : string
Return the database name.
getManager()  : Manager
Return the Manager.
getNamespace()  : string
Return the collection namespace.
getReadConcern()  : ReadConcern
Return the read concern for this collection.
getReadPreference()  : ReadPreference
Return the read preference for this collection.
getTypeMap()  : array<string|int, mixed>
Return the type map for this collection.
getWriteConcern()  : WriteConcern
Return the write concern for this collection.
insertMany()  : InsertManyResult
Inserts multiple documents.
insertOne()  : InsertOneResult
Inserts one document.
listIndexes()  : IndexInfoIterator
Returns information for all indexes for the collection.
mapReduce()  : MapReduceResult
Executes a map-reduce aggregation on the collection.
rename()  : array<string|int, mixed>|object
Renames the collection.
replaceOne()  : UpdateResult
Replaces at most one document matching the filter.
updateMany()  : UpdateResult
Updates all documents matching the filter.
updateOne()  : UpdateResult
Updates at most one document matching the filter.
watch()  : ChangeStream
Create a change stream for watching changes to the collection.
withOptions()  : Collection
Get a clone of this collection with different options.

Properties

$defaultTypeMap

private static array<string|int, mixed> $defaultTypeMap = ['array' => \MongoDB\Model\BSONArray::class, 'document' => \MongoDB\Model\BSONDocument::class, 'root' => \MongoDB\Model\BSONDocument::class]

$readPreference

private ReadPreference $readPreference

$typeMap

private array<string|int, mixed> $typeMap

$wireVersionForReadConcernWithWriteStage

private static int $wireVersionForReadConcernWithWriteStage = 8

$writeConcern

private WriteConcern $writeConcern

Methods

__construct()

Constructs new Collection instance.

public __construct(Manager $manager, string $databaseName, string $collectionName[, array<string|int, mixed> $options = [] ]) : mixed

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 : Manager

Manager instance from the driver

$databaseName : string

Database name

$collectionName : string

Collection name

$options : array<string|int, mixed> = []

Collection options

Tags
throws
InvalidArgumentException

for parameter/option parsing errors

Return values
mixed

aggregate()

Executes an aggregation framework pipeline on the collection.

public aggregate(array<string|int, mixed> $pipeline[, array<string|int, mixed> $options = [] ]) : Traversable

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.

Parameters
$pipeline : array<string|int, mixed>

List of pipeline operations

$options : array<string|int, mixed> = []

Command options

Tags
see
Aggregate::__construct()

for supported options

throws
UnexpectedValueException

if the command response was malformed

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
Traversable

bulkWrite()

Executes multiple write operations.

public bulkWrite(array<string|int, array<string|int, mixed>> $operations[, array<string|int, mixed> $options = [] ]) : BulkWriteResult
Parameters
$operations : array<string|int, array<string|int, mixed>>

List of write operations

$options : array<string|int, mixed> = []

Command options

Tags
see
BulkWrite::__construct()

for supported options

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
BulkWriteResult

count()

Gets the number of documents matching the filter.

public count([array<string|int, mixed>|object $filter = [] ][, array<string|int, mixed> $options = [] ]) : int
Parameters
$filter : array<string|int, mixed>|object = []

Query by which to filter documents

$options : array<string|int, mixed> = []

Command options

Tags
see
Count::__construct()

for supported options

throws
UnexpectedValueException

if the command response was malformed

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

deprecated
1.4
Return values
int

countDocuments()

Gets the number of documents matching the filter.

public countDocuments([array<string|int, mixed>|object $filter = [] ][, array<string|int, mixed> $options = [] ]) : int
Parameters
$filter : array<string|int, mixed>|object = []

Query by which to filter documents

$options : array<string|int, mixed> = []

Command options

Tags
see
CountDocuments::__construct()

for supported options

throws
UnexpectedValueException

if the command response was malformed

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
int

createIndex()

Create a single index for the collection.

public createIndex(array<string|int, mixed>|object $key[, array<string|int, mixed> $options = [] ]) : string
Parameters
$key : array<string|int, mixed>|object

Document containing fields mapped to values, which denote order or an index type

$options : array<string|int, mixed> = []

Index and command options

Tags
see
Collection::createIndexes()
see
CreateIndexes::__construct()

for supported command options

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
string

The name of the created index

createIndexes()

Create one or more indexes for the collection.

public createIndexes(array<string|int, array<string|int, mixed>> $indexes[, array<string|int, mixed> $options = [] ]) : array<string|int, string>

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.

Parameters
$indexes : array<string|int, array<string|int, mixed>>

List of index specifications

$options : array<string|int, mixed> = []

Command options

Tags
see
http://docs.mongodb.org/manual/reference/command/createIndexes/
see
http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
see
CreateIndexes::__construct()

for supported command options

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, string>

The names of the created indexes

deleteMany()

Deletes all documents matching the filter.

public deleteMany(array<string|int, mixed>|object $filter[, array<string|int, mixed> $options = [] ]) : DeleteResult
Parameters
$filter : array<string|int, mixed>|object

Query by which to delete documents

$options : array<string|int, mixed> = []

Command options

Tags
see
DeleteMany::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/delete/
throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
DeleteResult

deleteOne()

Deletes at most one document matching the filter.

public deleteOne(array<string|int, mixed>|object $filter[, array<string|int, mixed> $options = [] ]) : DeleteResult
Parameters
$filter : array<string|int, mixed>|object

Query by which to delete documents

$options : array<string|int, mixed> = []

Command options

Tags
see
DeleteOne::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/delete/
throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
DeleteResult

distinct()

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

public distinct(string $fieldName[, array<string|int, mixed>|object $filter = [] ][, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>
Parameters
$fieldName : string

Field for which to return distinct values

$filter : array<string|int, mixed>|object = []

Query by which to filter documents

$options : array<string|int, mixed> = []

Command options

Tags
see
Distinct::__construct()

for supported options

throws
UnexpectedValueException

if the command response was malformed

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>

drop()

Drop this collection.

public drop([array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object
Parameters
$options : array<string|int, mixed> = []

Additional options

Tags
see
DropCollection::__construct()

for supported options

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object

Command result document

dropIndex()

Drop a single index in the collection.

public dropIndex(string|IndexInfo $indexName[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object
Parameters
$indexName : string|IndexInfo

Index name or model object

$options : array<string|int, mixed> = []

Additional options

Tags
see
DropIndexes::__construct()

for supported options

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object

Command result document

dropIndexes()

Drop all indexes in the collection.

public dropIndexes([array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object
Parameters
$options : array<string|int, mixed> = []

Additional options

Tags
see
DropIndexes::__construct()

for supported options

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object

Command result document

estimatedDocumentCount()

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

public estimatedDocumentCount([array<string|int, mixed> $options = [] ]) : int
Parameters
$options : array<string|int, mixed> = []

Command options

Tags
see
EstimatedDocumentCount::__construct()

for supported options

throws
UnexpectedValueException

if the command response was malformed

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
int

explain()

Explains explainable commands.

public explain(Explainable $explainable[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object
Parameters
$explainable : Explainable

Command on which to run explain

$options : array<string|int, mixed> = []

Additional options

Tags
see
Explain::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/explain/
throws
UnsupportedException

if explainable or options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object

find()

Finds documents matching the query.

public find([array<string|int, mixed>|object $filter = [] ][, array<string|int, mixed> $options = [] ]) : Cursor
Parameters
$filter : array<string|int, mixed>|object = []

Query by which to filter documents

$options : array<string|int, mixed> = []

Additional options

Tags
see
Find::__construct()

for supported options

see
http://docs.mongodb.org/manual/core/read-operations-introduction/
throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
Cursor

findOne()

Finds a single document matching the query.

public findOne([array<string|int, mixed>|object $filter = [] ][, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object|null
Parameters
$filter : array<string|int, mixed>|object = []

Query by which to filter documents

$options : array<string|int, mixed> = []

Additional options

Tags
see
FindOne::__construct()

for supported options

see
http://docs.mongodb.org/manual/core/read-operations-introduction/
throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object|null

findOneAndDelete()

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

public findOneAndDelete(array<string|int, mixed>|object $filter[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object|null

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

Parameters
$filter : array<string|int, mixed>|object

Query by which to filter documents

$options : array<string|int, mixed> = []

Command options

Tags
see
FindOneAndDelete::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/findAndModify/
throws
UnexpectedValueException

if the command response was malformed

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object|null

findOneAndReplace()

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

public findOneAndReplace(array<string|int, mixed>|object $filter, array<string|int, mixed>|object $replacement[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object|null

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.

Parameters
$filter : array<string|int, mixed>|object

Query by which to filter documents

$replacement : array<string|int, mixed>|object

Replacement document

$options : array<string|int, mixed> = []

Command options

Tags
see
FindOneAndReplace::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/findAndModify/
throws
UnexpectedValueException

if the command response was malformed

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object|null

findOneAndUpdate()

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

public findOneAndUpdate(array<string|int, mixed>|object $filter, array<string|int, mixed>|object $update[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object|null

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.

Parameters
$filter : array<string|int, mixed>|object

Query by which to filter documents

$update : array<string|int, mixed>|object

Update to apply to the matched document

$options : array<string|int, mixed> = []

Command options

Tags
see
FindOneAndReplace::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/findAndModify/
throws
UnexpectedValueException

if the command response was malformed

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object|null

getCollectionName()

Return the collection name.

public getCollectionName() : string
Return values
string

getDatabaseName()

Return the database name.

public getDatabaseName() : string
Return values
string

getManager()

Return the Manager.

public getManager() : Manager
Return values
Manager

getReadPreference()

Return the read preference for this collection.

public getReadPreference() : ReadPreference
Return values
ReadPreference

getTypeMap()

Return the type map for this collection.

public getTypeMap() : array<string|int, mixed>
Return values
array<string|int, mixed>

insertMany()

Inserts multiple documents.

public insertMany(array<string|int, array<string|int, mixed>>|array<string|int, object> $documents[, array<string|int, mixed> $options = [] ]) : InsertManyResult
Parameters
$documents : array<string|int, array<string|int, mixed>>|array<string|int, object>

The documents to insert

$options : array<string|int, mixed> = []

Command options

Tags
see
InsertMany::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/insert/
throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
InsertManyResult

insertOne()

Inserts one document.

public insertOne(array<string|int, mixed>|object $document[, array<string|int, mixed> $options = [] ]) : InsertOneResult
Parameters
$document : array<string|int, mixed>|object

The document to insert

$options : array<string|int, mixed> = []

Command options

Tags
see
InsertOne::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/insert/
throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
InsertOneResult

mapReduce()

Executes a map-reduce aggregation on the collection.

public mapReduce(JavascriptInterface $map, JavascriptInterface $reduce, string|array<string|int, mixed>|object $out[, array<string|int, mixed> $options = [] ]) : MapReduceResult
Parameters
$map : JavascriptInterface

Map function

$reduce : JavascriptInterface

Reduce function

$out : string|array<string|int, mixed>|object

Output specification

$options : array<string|int, mixed> = []

Command options

Tags
see
MapReduce::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/mapReduce/
throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

throws
UnexpectedValueException

if the command response was malformed

Return values
MapReduceResult

rename()

Renames the collection.

public rename(string $toCollectionName[, string|null $toDatabaseName = null ][, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object
Parameters
$toCollectionName : string

New name of the collection

$toDatabaseName : string|null = null

New database name of the collection. Defaults to the original database.

$options : array<string|int, mixed> = []

Additional options

Tags
see
RenameCollection::__construct()

for supported options

throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
array<string|int, mixed>|object

Command result document

replaceOne()

Replaces at most one document matching the filter.

public replaceOne(array<string|int, mixed>|object $filter, array<string|int, mixed>|object $replacement[, array<string|int, mixed> $options = [] ]) : UpdateResult
Parameters
$filter : array<string|int, mixed>|object

Query by which to filter documents

$replacement : array<string|int, mixed>|object

Replacement document

$options : array<string|int, mixed> = []

Command options

Tags
see
ReplaceOne::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/update/
throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
UpdateResult

updateMany()

Updates all documents matching the filter.

public updateMany(array<string|int, mixed>|object $filter, array<string|int, mixed>|object $update[, array<string|int, mixed> $options = [] ]) : UpdateResult
Parameters
$filter : array<string|int, mixed>|object

Query by which to filter documents

$update : array<string|int, mixed>|object

Update to apply to the matched documents

$options : array<string|int, mixed> = []

Command options

Tags
see
UpdateMany::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/update/
throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
UpdateResult

updateOne()

Updates at most one document matching the filter.

public updateOne(array<string|int, mixed>|object $filter, array<string|int, mixed>|object $update[, array<string|int, mixed> $options = [] ]) : UpdateResult
Parameters
$filter : array<string|int, mixed>|object

Query by which to filter documents

$update : array<string|int, mixed>|object

Update to apply to the matched document

$options : array<string|int, mixed> = []

Command options

Tags
see
UpdateOne::__construct()

for supported options

see
http://docs.mongodb.org/manual/reference/command/update/
throws
UnsupportedException

if options are not supported by the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
UpdateResult

watch()

Create a change stream for watching changes to the collection.

public watch([array<string|int, mixed> $pipeline = [] ][, array<string|int, mixed> $options = [] ]) : ChangeStream
Parameters
$pipeline : array<string|int, mixed> = []

List of pipeline operations

$options : array<string|int, mixed> = []

Command options

Tags
see
Watch::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
ChangeStream

Search results