Documentation

Database
in package
implements Stringable

Tags
psalm-no-seal-properties

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
psalm-var

Encoder<array|stdClass|Document|PackedArray, mixed>

$databaseName

private string $databaseName

$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
throws
InvalidArgumentException

for parameter/option parsing errors

__toString()

Return the database name.

public __toString() : string
Return values
string

aggregate()

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
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
CursorInterface

command()

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
see
DatabaseCommand::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

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

Return values
CursorInterface

createCollection()

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
see
CreateCollection::__construct()

for supported options

see
https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.md#create-collection-helper
see
https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/manage-collections/
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)

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
see
CreateCollection::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

throws
CreateEncryptedCollectionException

for any errors creating data keys or creating the collection

throws
UnsupportedException

if Queryable Encryption is not supported by the selected server

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
see
DropDatabase::__construct()

for supported options

throws
UnsupportedException

if options are unsupported on the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

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

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
see
DropCollection::__construct()

for supported options

throws
UnsupportedException

if options are unsupported on the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

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

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
see
Collection::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
Collection

getDatabaseName()

Returns the database name.

public getDatabaseName() : string
Return values
string

getGridFSBucket()

Returns a GridFS bucket instance.

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

Bucket constructor options

Tags
see
Bucket::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
Bucket

getManager()

Return the Manager.

public getManager() : Manager
Return values
Manager

getReadPreference()

Return the read preference for this database.

public getReadPreference() : ReadPreference
Return values
ReadPreference

getTypeMap()

Return the type map for this database.

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

listCollectionNames()

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
see
ListCollectionNames::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

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

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
see
ListCollections::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

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

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
see
ModifyCollection::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

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

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

renameCollection()

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
see
RenameCollection::__construct()

for supported options

throws
UnsupportedException

if options are unsupported on the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

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

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
see
Collection::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
Collection

selectGridFSBucket()

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
see
Bucket::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
Bucket

watch()

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
see
Watch::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
ChangeStream

withOptions()

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

Tags
see
Database::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
Database

        
On this page

Search results