Skip to content

Commit 9165d44

Browse files
committed
first draft
1 parent bdd0fca commit 9165d44

File tree

5 files changed

+161
-1
lines changed

5 files changed

+161
-1
lines changed

snooty.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ php-library = "MongoDB PHP Library"
2828
php-library = "MongoDB PHP Library"
2929
driver-short = "PHP library"
3030
mdb-server = "MongoDB Server"
31-
driver-short = "PHP library"
3231
api = "https://www.mongodb.com/docs/php-library/current/reference"
32+
stable-api = "Stable API"

source/connect/connection-targets.txt

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
.. _php-connection-targets:
2+
3+
==========================
4+
Choose a Connection Target
5+
==========================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: connection string, URI, server, settings, client, stable api
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to use a connection string and ``MongoDB\Client`` object
24+
to connect to different types of MongoDB deployments.
25+
26+
.. _php-connection-atlas:
27+
28+
Atlas
29+
-----
30+
31+
To connect to a MongoDB deployment on Atlas, include the following elements
32+
in your connection string:
33+
34+
- URI of your Atlas cluster
35+
- MongoDB username
36+
- MongoDB password
37+
38+
Then, pass your connection string to the ``MongoDB\Client`` constructor.
39+
40+
When you connect to Atlas, we recommend using the {+stable-api+} client option to avoid
41+
breaking changes when Atlas upgrades to a new version of {+mdb-server+}.
42+
To learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page
43+
<php-stable-api>`.
44+
45+
The following code shows how to use the {+driver-short+} to connect to an Atlas cluster.
46+
47+
The code also uses the ``server_api_opts`` option to specify a {+stable-api+} version.
48+
49+
.. literalinclude:: /includes/connect/atlas.php
50+
:copyable: true
51+
:language: php
52+
53+
.. tip::
54+
55+
Follow the :atlas:`Atlas driver connection guide </driver-connection>`
56+
to retrieve your connection string.
57+
58+
.. _php-connection-local:
59+
60+
Local Deployments
61+
-----------------
62+
63+
To connect to a local MongoDB deployment, use ``localhost`` as the hostname. By
64+
default, the ``mongod`` process runs on port 27017, though you can customize this for
65+
your deployment.
66+
67+
The following code shows how to use the {+driver-short+} to connect to a local MongoDB
68+
deployment:
69+
70+
.. literalinclude:: /includes/connect/client.php
71+
:language: php
72+
:copyable: true
73+
74+
.. _php-connection-replica-set:
75+
76+
Replica Sets
77+
------------
78+
79+
To connect to a replica set, specify the hostnames (or IP addresses) and
80+
port numbers of the replica set members in your connection string.
81+
82+
If you aren't able to provide a full list of hosts in the replica set, you can
83+
specify one or more of the hosts in the replica set and instruct the {+driver-short+} to
84+
perform automatic discovery to find the others. To instruct the driver to perform
85+
automatic discovery, perform one of the following actions:
86+
87+
- Specify the name of the replica set as the value of the ``replicaSet`` parameter.
88+
- Specify ``false`` as the value of the ``directConnection`` parameter.
89+
- Specify more than one host in the replica set.
90+
91+
In the following example, the driver uses a sample connection URI to connect to the
92+
MongoDB replica set ``sampleRS``, which is running on port ``27017`` of three different
93+
hosts, including ``host1``:
94+
95+
.. literalinclude:: /includes/connect/replica-set.php
96+
:language: php
97+
:copyable: true
98+
99+
Initialization
100+
~~~~~~~~~~~~~~
101+
102+
To initialize a replica set, you must connect directly to a single member. To do so,
103+
set the ``directConnection`` connection
104+
option to ``true`` in the connection string. The following code example shows how to
105+
set this connection option:
106+
107+
.. literalinclude:: /includes/connect/direct-connection.php
108+
:language: php
109+
:copyable: true
110+
111+
API Documentation
112+
-----------------
113+
114+
To learn more about using the ``MongoDB\Client`` class,
115+
see the following API documentation:
116+
117+
- :ref:`MongoDB\Client <php-api-mongodbclient>`

source/includes/connect/atlas.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
require 'vendor/autoload.php'; // Make sure you have installed the MongoDB library via Composer
3+
4+
use MongoDB\Client;
5+
use MongoDB\Driver\Exception\Exception;
6+
7+
try {
8+
// Replace the placeholder with your Atlas connection string
9+
$uri = "<connection string>";
10+
11+
// Create a MongoDB client with server API options
12+
$client = new Client($uri, [], [
13+
'serverApi' => [
14+
'version' => '1'
15+
]
16+
]);
17+
18+
// Ping the server to verify that the connection works
19+
$admin = $client->admin;
20+
$command = new MongoDB\Driver\Command(['ping' => 1]);
21+
$result = $admin->command($command)->toArray();
22+
23+
echo json_encode($result), "\n";
24+
echo "Pinged your deployment. You successfully connected to MongoDB!\n";
25+
} catch (Exception $e) {
26+
echo "An exception occurred: ", $e->getMessage(), "\n";
27+
exit(EXIT_FAILURE);
28+
}
29+
?>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Replace the placeholders with your actual hostname and port
4+
$uri = "mongodb://<hostname>:<port>/?directConnection=true";
5+
6+
// Create a MongoDB client
7+
$client = new Client($uri);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Replace the placeholder with your connection string
4+
$uri = "mongodb://host1:27017/?replicaSet=sampleRS";
5+
6+
// Create a MongoDB client
7+
$client = new Client($uri);

0 commit comments

Comments
 (0)