Skip to content

Connect to your database

Thomas Cocchiara edited this page Jan 5, 2018 · 3 revisions

Previous | Next

Connect to your database

To connect the application to your mongo database you should first retrieve the connection string. The format for connection strings is:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

For more information see the link: https://docs.mongodb.com/manual/reference/connection-string/

To connect locally usually the connection string is:

mongodb://localhost:27017

Once you have the connection string simply instantiate a MongoDriver\Adapter like so

use MongoDriver\Adapter;

$adapter = new Adapter();
$adapter->connect("mongodb://localhost:2017"");

And then select the database

$adapter->selectDB("myDatabase");

Or simply select the db during the connection:

$adapter->connect("mongodb://localhost:2017", "myDatabase");

Previous | Next

Clone this wiki locally