Connecting to standalone MongoDB node:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://example.com:27017");
?>
Connecting to standalone MongoDB node via a Unix domain socket. The socket
path may include special characters such as slashes and should be encoded
with rawurlencode().
<?php
$manager = new MongoDB\Driver\Manager("mongodb://" . rawurlencode("/tmp/mongodb-27017.sock"));
?>
Connecting to a replica set:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet");
?>
Connecting to a sharded cluster (i.e. one or more mongos instances):
<?php
$manager = new MongoDB\Driver\Manager("mongodb://mongos1.example.com,mongos2.example.com/");
?>
Connecting to MongoDB with authentication credentials for a particular user and database:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://myusername:mypassword@example.com/mydatabase");
?>
Connecting to MongoDB with authentication credentials for a particular
user and database, where the username or password includes special
characters (e.g. @, :,
%). In the following example, the password string
myp@ss:w%rd has been manually escaped; however,
rawurlencode() may also be used to escape URI components
that may contain special characters.
<?php
$manager = new MongoDB\Driver\Manager("mongodb://myusername:myp%40ss%3Aw%25rd@example.com/mydatabase");
?>
Connecting to MongoDB with X509 authentication:
<?php
$manager = new MongoDB\Driver\Manager(
"mongodb://example.com/?ssl=true&authMechanism=MONGODB-X509",
[],
[
"pem_file" => "/path/to/client.pem",
]
);
?>