Database
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
- $databaseName : string
- $manager : Manager
- $readConcern : ReadConcern
- $readPreference : ReadPreference
- $typeMap : array<string|int, mixed>
- $writeConcern : WriteConcern
Methods
- __construct() : mixed
- Constructs new Database instance.
- __debugInfo() : array<string|int, mixed>
- Return internal properties for debugging purposes.
- __get() : Collection
- Select a collection within this database.
- __toString() : string
- Return the database name.
- aggregate() : CursorInterface
- Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as $currentOp and $listLocalSessions. Requires MongoDB >= 3.6
- command() : CursorInterface
- Execute a command on this database.
- createCollection() : void
- Create a new collection explicitly.
- createEncryptedCollection() : array<string|int, mixed>
- Create a new encrypted collection explicitly.
- drop() : void
- Drop this database.
- dropCollection() : void
- Drop a collection within this database.
- getCollection() : Collection
- Returns a collection instance.
- getDatabaseName() : string
- Returns the database name.
- getGridFSBucket() : Bucket
- Returns a GridFS bucket instance.
- getManager() : Manager
- Return the Manager.
- getReadConcern() : ReadConcern
- Return the read concern for this database.
- getReadPreference() : ReadPreference
- Return the read preference for this database.
- getTypeMap() : array<string|int, mixed>
- Return the type map for this database.
- getWriteConcern() : WriteConcern
- Return the write concern for this database.
- listCollectionNames() : Iterator<int, string>
- Returns the names of all collections in this database
- listCollections() : Iterator<int, CollectionInfo>
- Returns information for all collections in this database.
- modifyCollection() : array<string|int, mixed>|object
- Modifies a collection or view.
- renameCollection() : void
- Rename a collection within this database.
- selectCollection() : Collection
- Select a collection within this database.
- selectGridFSBucket() : Bucket
- Select a GridFS bucket within this database.
- watch() : ChangeStream
- Create a change stream for watching changes to the database.
- withOptions() : Database
- Get a clone of this database with different options.
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
$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 Database instance.
public
__construct(Manager $manager, string $databaseName[, array<string|int, mixed> $options = [] ]) : mixed
This class provides methods for database-specific operations and serves as a gateway for accessing collections.
Supported options:
-
builderEncoder (MongoDB\Codec\Encoder): Encoder for query and aggregation builders. If not given, the default encoder will be used.
-
readConcern (MongoDB\Driver\ReadConcern): The default read concern to use for database operations and selected collections. Defaults to the Manager's read concern.
-
readPreference (MongoDB\Driver\ReadPreference): The default read preference to use for database operations and selected collections. 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 database operations and selected collections. Defaults to the Manager's write concern.
Parameters
- $manager : Manager
-
Manager instance from the driver
- $databaseName : string
-
Database name
- $options : array<string|int, mixed> = []
-
Database options
Tags
__debugInfo()
Return internal properties for debugging purposes.
public
__debugInfo() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>__get()
Select a collection within this database.
public
__get(string $collectionName) : Collection
Note: collections whose names contain special characters (e.g. ".") may be selected with complex syntax (e.g. $database->{"system.profile"}) or .
Parameters
- $collectionName : string
-
Name of the collection to select
Tags
Return values
Collection__toString()
Return the database name.
public
__toString() : string
Return values
stringaggregate()
Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as $currentOp and $listLocalSessions. Requires MongoDB >= 3.6
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
CursorInterfacecommand()
Execute a command on this database.
public
command(array<string|int, mixed>|object $command[, array<string|int, mixed> $options = [] ]) : CursorInterface
Parameters
- $command : array<string|int, mixed>|object
-
Command document
- $options : array<string|int, mixed> = []
-
Options for command execution
Tags
Return values
CursorInterfacecreateCollection()
Create a new collection explicitly.
public
createCollection(string $collectionName[, array<string|int, mixed> $options = [] ]) : void
If the "encryptedFields" option is specified, this method additionally creates related metadata collections and an index on the encrypted collection.
Parameters
- $collectionName : string
- $options : array<string|int, mixed> = []
Tags
createEncryptedCollection()
Create a new encrypted collection explicitly.
public
createEncryptedCollection(string $collectionName, ClientEncryption $clientEncryption, string $kmsProvider, array<string|int, mixed>|null $masterKey, array<string|int, mixed> $options) : array<string|int, mixed>
The "encryptedFields" option is required.
This method will automatically create data keys for any encrypted fields where "keyId" is null. A copy of the modified "encryptedFields" option will be returned in addition to the result from creating the collection.
If any error is encountered creating data keys or the collection, a CreateEncryptedCollectionException will be thrown. The original exception and modified "encryptedFields" option can be accessed via the getPrevious() and getEncryptedFields() methods, respectively.
Parameters
- $collectionName : string
- $clientEncryption : ClientEncryption
- $kmsProvider : string
- $masterKey : array<string|int, mixed>|null
- $options : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —The modified "encryptedFields" option
drop()
Drop this database.
public
drop([array<string|int, mixed> $options = [] ]) : void
Parameters
- $options : array<string|int, mixed> = []
-
Additional options
Tags
dropCollection()
Drop a collection within this database.
public
dropCollection(string $collectionName[, array<string|int, mixed> $options = [] ]) : void
Parameters
- $collectionName : string
-
Collection name
- $options : array<string|int, mixed> = []
-
Additional options
Tags
getCollection()
Returns a collection instance.
public
getCollection(string $collectionName[, array<string|int, mixed> $options = [] ]) : Collection
If the collection does not exist in the database, it is not created when invoking this method.
Parameters
- $collectionName : string
- $options : array<string|int, mixed> = []
Tags
Return values
CollectiongetDatabaseName()
Returns the database name.
public
getDatabaseName() : string
Return values
stringgetGridFSBucket()
Returns a GridFS bucket instance.
public
getGridFSBucket([array<string|int, mixed> $options = [] ]) : Bucket
Parameters
- $options : array<string|int, mixed> = []
-
Bucket constructor options
Tags
Return values
BucketgetManager()
Return the Manager.
public
getManager() : Manager
Return values
ManagergetReadConcern()
Return the read concern for this database.
public
getReadConcern() : ReadConcern
Tags
Return values
ReadConcerngetReadPreference()
Return the read preference for this database.
public
getReadPreference() : ReadPreference
Return values
ReadPreferencegetTypeMap()
Return the type map for this database.
public
getTypeMap() : array<string|int, mixed>
Return values
array<string|int, mixed>getWriteConcern()
Return the write concern for this database.
public
getWriteConcern() : WriteConcern
Tags
Return values
WriteConcernlistCollectionNames()
Returns the names of all collections in this database
public
listCollectionNames([array<string|int, mixed> $options = [] ]) : Iterator<int, string>
Parameters
- $options : array<string|int, mixed> = []
Tags
Return values
Iterator<int, string>listCollections()
Returns information for all collections in this database.
public
listCollections([array<string|int, mixed> $options = [] ]) : Iterator<int, CollectionInfo>
Parameters
- $options : array<string|int, mixed> = []
Tags
Return values
Iterator<int, CollectionInfo>modifyCollection()
Modifies a collection or view.
public
modifyCollection(string $collectionName, array<string|int, mixed> $collectionOptions[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|object
Parameters
- $collectionName : string
-
Collection or view to modify
- $collectionOptions : array<string|int, mixed>
-
Collection or view options to assign
- $options : array<string|int, mixed> = []
-
Command options
Tags
Return values
array<string|int, mixed>|objectrenameCollection()
Rename a collection within this database.
public
renameCollection(string $fromCollectionName, string $toCollectionName[, string|null $toDatabaseName = null ][, array<string|int, mixed> $options = [] ]) : void
Parameters
- $fromCollectionName : string
-
Collection name
- $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
selectCollection()
Select a collection within this database.
public
selectCollection(string $collectionName[, array<string|int, mixed> $options = [] ]) : Collection
Parameters
- $collectionName : string
-
Name of the collection to select
- $options : array<string|int, mixed> = []
-
Collection constructor options
Tags
Return values
CollectionselectGridFSBucket()
Select a GridFS bucket within this database.
public
selectGridFSBucket([array<string|int, mixed> $options = [] ]) : Bucket
Parameters
- $options : array<string|int, mixed> = []
-
Bucket constructor options
Tags
Return values
Bucketwatch()
Create a change stream for watching changes to the database.
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 database with different options.
public
withOptions([array<string|int, mixed> $options = [] ]) : Database
Parameters
- $options : array<string|int, mixed> = []
-
Database constructor options