-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41955: Connect to MongoDB #100
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
norareidy
merged 8 commits into
mongodb:php-standardization
from
norareidy:DOCSP-41955-connect-to-mongodb
Aug 27, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cfb9d31
DOCSP-41955: Connect to MongoDB
norareidy 13ea674
edits
norareidy a69a805
edit
norareidy f4c0de6
Merge remote-tracking branch 'upstream/php-standardization' into DOCS…
norareidy 6a1cd30
RR feedback
norareidy bd636ab
typo
norareidy ca2f634
code edit
norareidy 71c5411
Merge remote-tracking branch 'upstream/php-standardization' into DOCS…
norareidy 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,69 @@ | ||
.. _php-connect-to-mongodb: | ||
|
||
================== | ||
Connect to MongoDB | ||
================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: tutorial | ||
|
||
.. meta:: | ||
:keywords: test connection, runnable, code example | ||
|
||
After retrieving the connection string for your MongoDB Atlas deployment, | ||
you can connect to the deployment from your PHP application and query | ||
the Atlas sample datasets. | ||
|
||
.. procedure:: | ||
:style: connected | ||
|
||
.. step:: Edit your PHP application file | ||
|
||
Copy and paste the following code into the ``quickstart.php`` file, which queries | ||
the ``movies`` collection in the ``sample_mflix`` database: | ||
|
||
.. literalinclude:: /includes/get-started/quickstart.php | ||
:language: php | ||
:dedent: | ||
|
||
.. step:: Assign the connection string | ||
|
||
Replace the ``<connection string>`` placeholder with the | ||
connection string that you copied from the :ref:`php-connection-string` | ||
step of this guide. | ||
|
||
.. step:: Run your PHP application | ||
|
||
In your project directory, run the following shell command to start the application: | ||
|
||
.. code-block:: bash | ||
|
||
php quickstart.php | ||
|
||
The command line output contains details about the retrieved movie | ||
document: | ||
|
||
.. code-block:: none | ||
:copyable: false | ||
|
||
{ | ||
"_id": { | ||
"$oid": "..." | ||
}, | ||
... | ||
"rated": "R", | ||
"metacritic": 80, | ||
"title": "The Shawshank Redemption", | ||
... | ||
} | ||
|
||
If you encounter an error or see no output, ensure that you specified the | ||
proper connection string in the ``quickstart.php`` file and that you loaded the | ||
sample data. | ||
|
||
After you complete these steps, you have a PHP application that | ||
connects to your MongoDB deployment, runs a query on the sample | ||
data, and returns a matching document. | ||
|
||
.. include:: /includes/get-started/troubleshoot.rst |
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,17 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use MongoDB\Client; | ||
|
||
$client = new Client('<connection string>'); | ||
$collection = $client->sample_mflix->movies; | ||
|
||
$filter = ['title' => 'The Shawshank Redemption']; | ||
$result = $collection->findOne($filter); | ||
|
||
if ($result) { | ||
echo json_encode($result, JSON_PRETTY_PRINT); | ||
} else { | ||
echo "Document not found"; | ||
} |
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.
S: As in the other pages, I think it would be good to add a short introduction about what is achieved in this step here