-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41958 - Connection Targets #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mongoKart
merged 9 commits into
mongodb:php-standardization
from
mongoKart:docsp-41958-connection-targets
Sep 6, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4bc9856
first draft
mongoKart 621622b
fixes
mongoKart 000ac25
fixes
mongoKart 4ef5647
qualify type names
mongoKart 3b15c26
Apply suggestions from code review
mongoKart 6c9011a
nr feedback
mongoKart 5ecaa22
Apply suggestions from code review
mongoKart e19330c
feedback
mongoKart bd15ec0
Merge branch 'php-standardization' into docsp-41958-connection-targets
mongoKart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
.. _php-connection-targets: | ||
|
||
========================== | ||
Choose a Connection Target | ||
========================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: connection string, URI, server, settings, client, stable api | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use a connection string and ``MongoDB\Client`` object | ||
to connect to different types of MongoDB deployments. | ||
|
||
.. _php-connection-atlas: | ||
|
||
Atlas | ||
----- | ||
|
||
To connect to a MongoDB deployment on Atlas, include the following elements | ||
in your connection string: | ||
|
||
- URI of your Atlas cluster | ||
- Database username | ||
- Database user's password | ||
|
||
Then, pass your connection string to the ``MongoDB\Client`` constructor. | ||
|
||
When you connect to Atlas, we recommend using the {+stable-api+} client option to avoid | ||
breaking changes when Atlas upgrades to a new version of {+mdb-server+}. | ||
To learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page | ||
<php-stable-api>`. | ||
|
||
The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. | ||
The code also uses the ``serverApi`` option to specify a {+stable-api+} version. | ||
|
||
.. literalinclude:: /includes/connect/atlas.php | ||
:copyable: true | ||
:language: php | ||
|
||
.. tip:: | ||
|
||
Follow the :ref:`php-connection-string` step of the Quick Start | ||
to retrieve your connection string. | ||
|
||
.. _php-connection-local: | ||
|
||
Local Deployments | ||
----------------- | ||
|
||
To connect to a local MongoDB deployment, use ``localhost`` as the hostname. By | ||
default, the ``mongod`` process runs on port 27017, though you can customize this for | ||
your deployment. | ||
|
||
The following code shows how to use the {+driver-short+} to connect to a local MongoDB | ||
deployment: | ||
|
||
.. literalinclude:: /includes/connect/client.php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. broken include for now |
||
:language: php | ||
:copyable: true | ||
|
||
.. _php-connection-replica-set: | ||
|
||
Replica Sets | ||
------------ | ||
|
||
To connect to a replica set, specify the hostnames (or IP addresses) and | ||
port numbers of the replica set members in your connection string. | ||
|
||
If you aren't able to provide a full list of hosts in the replica set, you can | ||
specify one or more of the hosts in the replica set and instruct the {+driver-short+} to | ||
perform automatic discovery to find the others. To instruct the driver to perform | ||
automatic discovery, choose one of the following actions: | ||
|
||
- Specify the name of the replica set as the value of the ``replicaSet`` parameter. | ||
- Specify ``false`` as the value of the ``directConnection`` parameter. | ||
- Specify more than one host in the replica set. | ||
|
||
In the following example, the driver uses a sample connection URI to connect to the | ||
MongoDB replica set ``sampleRS``, which is running on port ``27017`` of three different | ||
hosts, including ``host1``: | ||
|
||
.. literalinclude:: /includes/connect/replica-set.php | ||
:language: php | ||
:copyable: true | ||
|
||
Initialization | ||
~~~~~~~~~~~~~~ | ||
|
||
To initialize a replica set, you must connect directly to a single member. To do so, | ||
set the ``directConnection`` connection | ||
option to ``true`` in the connection string. The following code example shows how to | ||
set this connection option: | ||
|
||
.. literalinclude:: /includes/connect/direct-connection.php | ||
:language: php | ||
:copyable: true | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about using the ``MongoDB\Client`` class, | ||
see the following API documentation: | ||
|
||
- :ref:`MongoDB\Client <php-api-mongodbclient>` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
// Replace the placeholder with your Atlas connection string | ||
$uri = "<connection string>"; | ||
|
||
// Create a MongoDB client with server API options | ||
$client = new MongoDB\Client($uri, [], [ | ||
'serverApi' => new MongoDB\Driver\ServerApi('1') | ||
]); | ||
|
||
// Ping the server to verify that the connection works | ||
$admin = $client->admin; | ||
$command = new MongoDB\Driver\Command(['ping' => 1]); | ||
$result = $admin->command($command)->toArray(); | ||
|
||
echo json_encode($result), "\n"; | ||
echo "Pinged your deployment. You successfully connected to MongoDB!\n"; | ||
|
||
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
// Replace the placeholders with your actual hostname and port | ||
$uri = "mongodb://<hostname>:<port>/?directConnection=true"; | ||
|
||
// Create a MongoDB client | ||
$client = new MongoDB\Client($uri); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
$uri = "mongodb://host1:27017/?replicaSet=sampleRS"; | ||
|
||
// Create a MongoDB client | ||
$client = new MongoDB\Client($uri); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
broken link for now