PHP 8.0.6 Released!


(PECL mongo >=0.9.0)

MongoDB::__constructCreates a new database


public MongoDB::__construct ( MongoClient $conn , string $name )

This method is not meant to be called directly. The preferred way to create an instance of MongoDB is through MongoClient::__get() or MongoClient::selectDB().

If you're ignoring the previous paragraph and want to call it directly you can do so:


= new MongoClient();
$db = new MongoDB($m'mydbname');


But don't. Isn't this much nicer:


= new MongoClient();
$db $m->mydbname;

// or, if the name contains weird characters:

$db $m->selectDB('my,db:name');



MongoClient conn

Database connection.


Database name.

Return Values

Returns the database.


Throws default exception if the database name is invalid.

add a note add a note

User Contributed Notes 1 note

mike at eastghost dot com
7 years ago
To use DICE (Dependency Injection Container) you'll need to use this format

// $Mongo = new \MongoClient( arrConnectionOptions );
// $Mdb = new \MongoDB( $Mongo, 'dbName' );
$dice->create( '\Mongo' );
$dice->create( '\Mdb' );

and not this one

$Mdb = $Mongo->dbName;
To Top