Collection
in package
implements
Stringable
Tags
Table of Contents
Interfaces
- Stringable
Constants
- DEFAULT_TYPE_MAP = ['array' => \MongoDB\Model\BSONArray::class, 'document' => \MongoDB\Model\BSONDocument::class, 'root' => \MongoDB\Model\BSONDocument::class]
Properties
- $autoEncryptionEnabled : bool
- $builderEncoder : Encoder
- $codec : DocumentCodec|null
- $collectionName : string
- $databaseName : string
- $manager : Manager
- $readConcern : ReadConcern
- $readPreference : ReadPreference
- $typeMap : array<string|int, mixed>
- $writeConcern : WriteConcern
Methods
- __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() : CursorInterface
- 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.
- createSearchIndex() : string
- Create an Atlas Search index for the collection.
- createSearchIndexes() : array<string|int, string>
- Create one or more Atlas Search 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() : void
- Drop this collection.
- dropIndex() : void
- Drop a single index in the collection.
- dropIndexes() : void
- Drop all indexes in the collection.
- dropSearchIndex() : void
- Drop a single Atlas Search index 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() : CursorInterface
- 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.
- getBuilderEncoder() : Encoder
- getCodec() : DocumentCodec|null
- 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() : Iterator<int, IndexInfo>
- Returns information for all indexes for the collection.
- listSearchIndexes() : Countable|Iterator
- Returns information for all Atlas Search indexes for the collection.
- rename() : void
- 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.
- updateSearchIndex() : void
- Update a single Atlas Search index in the collection.
- watch() : ChangeStream
- Create a change stream for watching changes to the collection.
- withOptions() : Collection
- Get a clone of this collection with different options.
- inheritBuilderEncoder() : array<string|int, mixed>
- inheritCodec() : array<string|int, mixed>
- inheritCodecOrTypeMap() : array<string|int, mixed>
- inheritReadConcern() : array<string|int, mixed>
- inheritReadOptions() : array<string|int, mixed>
- inheritReadPreference() : array<string|int, mixed>
- inheritTypeMap() : array<string|int, mixed>
- inheritWriteOptions() : array<string|int, mixed>
Constants
DEFAULT_TYPE_MAP
private
mixed
DEFAULT_TYPE_MAP
= ['array' => \MongoDB\Model\BSONArray::class, 'document' => \MongoDB\Model\BSONDocument::class, 'root' => \MongoDB\Model\BSONDocument::class]
Properties
$autoEncryptionEnabled
private
bool
$autoEncryptionEnabled
$builderEncoder read-only
private
Encoder
$builderEncoder
Tags
$codec
private
DocumentCodec|null
$codec
= null
$collectionName
private
string
$collectionName
$databaseName
private
string
$databaseName
$manager
private
Manager
$manager
$readConcern
private
ReadConcern
$readConcern
$readPreference
private
ReadPreference
$readPreference
$typeMap
private
array<string|int, mixed>
$typeMap
$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:
-
builderEncoder (MongoDB\Codec\Encoder): Encoder for query and aggregation builders. If not given, the default encoder will be used.
-
codec (MongoDB\Codec\DocumentCodec): Codec used to decode documents from BSON to PHP objects.
-
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
__debugInfo()
Return internal properties for debugging purposes.
public
__debugInfo() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>__toString()
Return the collection namespace (e.g. "db.collection").
public
__toString() : string
Tags
Return values
stringaggregate()
Executes an aggregation framework pipeline on the collection.
public
aggregate(array<string|int, mixed>|Pipeline $pipeline[, array<string|int, mixed> $options = [] ]) : CursorInterface
Parameters
- $pipeline : array<string|int, mixed>|Pipeline
-
Aggregation pipeline
- $options : array<string|int, mixed> = []
-
Command options
Tags
Return values
CursorInterfacebulkWrite()
Executes multiple write operations.
public
bulkWrite(array<int, array{deleteMany: list}|array{deleteOne: list}|array{insertOne: list}|array{replaceOne: list}|array{updateMany: list}|array{updateOne: list}> $operations[, array<string|int, mixed> $options = [] ]) : BulkWriteResult
Parameters
-
$operations
: array<int, array{deleteMany: list
}|array{deleteOne: list }|array{insertOne: list }|array{replaceOne: list }|array{updateMany: list }|array{updateOne: list }> -
List of write operations
- $options : array<string|int, mixed> = []
-
Command options
Tags
Return values
BulkWriteResultcount()
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
Return values
intcountDocuments()
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
Return values
intcreateIndex()
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
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
Return values
array<string|int, string> —The names of the created indexes
createSearchIndex()
Create an Atlas Search index for the collection.
public
createSearchIndex(array<string|int, mixed>|object $definition[, array{comment?: mixed, name?: string, type?: string} $options = [] ]) : string
Only available when used against a 7.0+ Atlas cluster.
Parameters
- $definition : array<string|int, mixed>|object
-
Atlas Search index mapping definition
- $options : array{comment?: mixed, name?: string, type?: string} = []
-
Index and command options
Tags
Return values
string —The name of the created search index
createSearchIndexes()
Create one or more Atlas Search indexes for the collection.
public
createSearchIndexes(array<int, array{definition: array|object, name?: string, type?: string}> $indexes[, array{comment?: mixed} $options = [] ]) : array<string|int, string>
Only available when used against a 7.0+ Atlas cluster.
Each element in the $indexes array must have "definition" document and they may have a "name" string. The name can be omitted for a single index, in which case a name will be the default. For example:
$indexes = [
// Create a search index with the default name on a single field
['definition' => ['mappings' => ['dynamic' => false, 'fields' => ['title' => ['type' => 'string']]]]],
// Create a named search index on all fields
['name' => 'search_all', 'definition' => ['mappings' => ['dynamic' => true]]],
];
Parameters
- $indexes : array<int, array{definition: array|object, name?: string, type?: string}>
-
List of search index specifications
- $options : array{comment?: mixed} = []
-
Command options
Tags
Return values
array<string|int, string> —The names of the created search 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
Return values
DeleteResultdeleteOne()
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
Return values
DeleteResultdistinct()
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
Return values
array<string|int, mixed>drop()
Drop this collection.
public
drop([array<string|int, mixed> $options = [] ]) : void
Parameters
- $options : array<string|int, mixed> = []
-
Additional options
Tags
dropIndex()
Drop a single index in the collection.
public
dropIndex(string|IndexInfo $indexName[, array<string|int, mixed> $options = [] ]) : void
Parameters
- $indexName : string|IndexInfo
-
Index name or model object
- $options : array<string|int, mixed> = []
-
Additional options
Tags
dropIndexes()
Drop all indexes in the collection.
public
dropIndexes([array<string|int, mixed> $options = [] ]) : void
Parameters
- $options : array<string|int, mixed> = []
-
Additional options
Tags
dropSearchIndex()
Drop a single Atlas Search index in the collection.
public
dropSearchIndex(string $name[, array{comment?: mixed} $options = [] ]) : void
Only available when used against a 7.0+ Atlas cluster.
Parameters
- $name : string
-
Search index name
- $options : array{comment?: mixed} = []
-
Additional options
Tags
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
Return values
intexplain()
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
Return values
array<string|int, mixed>|objectfind()
Finds documents matching the query.
public
find([array<string|int, mixed>|object $filter = [] ][, array<string|int, mixed> $options = [] ]) : CursorInterface
Parameters
- $filter : array<string|int, mixed>|object = []
-
Query by which to filter documents
- $options : array<string|int, mixed> = []
-
Additional options
Tags
Return values
CursorInterfacefindOne()
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
Return values
array<string|int, mixed>|object|nullfindOneAndDelete()
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
Return values
array<string|int, mixed>|object|nullfindOneAndReplace()
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
Return values
array<string|int, mixed>|object|nullfindOneAndUpdate()
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
Return values
array<string|int, mixed>|object|nullgetBuilderEncoder()
public
getBuilderEncoder() : Encoder
Tags
Return values
EncodergetCodec()
public
getCodec() : DocumentCodec|null
Return values
DocumentCodec|nullgetCollectionName()
Return the collection name.
public
getCollectionName() : string
Return values
stringgetDatabaseName()
Return the database name.
public
getDatabaseName() : string
Return values
stringgetManager()
Return the Manager.
public
getManager() : Manager
Return values
ManagergetNamespace()
Return the collection namespace.
public
getNamespace() : string
Tags
Return values
stringgetReadConcern()
Return the read concern for this collection.
public
getReadConcern() : ReadConcern
Tags
Return values
ReadConcerngetReadPreference()
Return the read preference for this collection.
public
getReadPreference() : ReadPreference
Return values
ReadPreferencegetTypeMap()
Return the type map for this collection.
public
getTypeMap() : array<string|int, mixed>
Return values
array<string|int, mixed>getWriteConcern()
Return the write concern for this collection.
public
getWriteConcern() : WriteConcern
Tags
Return values
WriteConcerninsertMany()
Inserts multiple documents.
public
insertMany(array<int, object|array<string|int, mixed>> $documents[, array<string|int, mixed> $options = [] ]) : InsertManyResult
Parameters
- $documents : array<int, object|array<string|int, mixed>>
-
The documents to insert
- $options : array<string|int, mixed> = []
-
Command options
Tags
Return values
InsertManyResultinsertOne()
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
Return values
InsertOneResultlistIndexes()
Returns information for all indexes for the collection.
public
listIndexes([array<string|int, mixed> $options = [] ]) : Iterator<int, IndexInfo>
Parameters
- $options : array<string|int, mixed> = []
Tags
Return values
Iterator<int, IndexInfo>listSearchIndexes()
Returns information for all Atlas Search indexes for the collection.
public
listSearchIndexes([array<string|int, mixed> $options = [] ]) : Countable|Iterator
Only available when used against a 7.0+ Atlas cluster.
Parameters
- $options : array<string|int, mixed> = []
-
Command options
Tags
Return values
Countable|Iteratorrename()
Renames the collection.
public
rename(string $toCollectionName[, string|null $toDatabaseName = null ][, array<string|int, mixed> $options = [] ]) : void
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
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
Return values
UpdateResultupdateMany()
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
Return values
UpdateResultupdateOne()
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
Return values
UpdateResultupdateSearchIndex()
Update a single Atlas Search index in the collection.
public
updateSearchIndex(string $name, array<string|int, mixed>|object $definition[, array{comment?: mixed} $options = [] ]) : void
Only available when used against a 7.0+ Atlas cluster.
Parameters
- $name : string
-
Search index name
- $definition : array<string|int, mixed>|object
-
Atlas Search index definition
- $options : array{comment?: mixed} = []
-
Command options
Tags
watch()
Create a change stream for watching changes to the collection.
public
watch([array<string|int, mixed>|Pipeline $pipeline = [] ][, array<string|int, mixed> $options = [] ]) : ChangeStream
Parameters
- $pipeline : array<string|int, mixed>|Pipeline = []
-
Aggregation pipeline
- $options : array<string|int, mixed> = []
-
Command options
Tags
Return values
ChangeStreamwithOptions()
Get a clone of this collection with different options.
public
withOptions([array<string|int, mixed> $options = [] ]) : Collection
Parameters
- $options : array<string|int, mixed> = []
-
Collection constructor options
Tags
Return values
CollectioninheritBuilderEncoder()
private
inheritBuilderEncoder(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
Return values
array<string|int, mixed>inheritCodec()
private
inheritCodec(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
Return values
array<string|int, mixed>inheritCodecOrTypeMap()
private
inheritCodecOrTypeMap(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
Return values
array<string|int, mixed>inheritReadConcern()
private
inheritReadConcern(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
Return values
array<string|int, mixed>inheritReadOptions()
private
inheritReadOptions(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
Return values
array<string|int, mixed>inheritReadPreference()
private
inheritReadPreference(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
Return values
array<string|int, mixed>inheritTypeMap()
private
inheritTypeMap(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
Return values
array<string|int, mixed>inheritWriteOptions()
private
inheritWriteOptions(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>