Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion source/get-started.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Get Started with the PHP Library
.. toctree::

/get-started/create-a-deployment/
/get-started/create-a-connection-string/
/get-started/create-a-connection-string/
/get-started/connect-to-mongodb/

65 changes: 65 additions & 0 deletions source/get-started/connect-to-mongodb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.. _php-connect-to-mongodb:

==================
Connect to MongoDB
==================

.. facet::
:name: genre
:values: tutorial

.. meta::
:keywords: test connection, runnable, code example

.. procedure::
:style: connected

Copy link
Contributor

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

.. 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 working application that
uses the {+php-library+} to connect to your MongoDB deployment, runs a query on
the sample data, and prints out the result.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: Even though the grammar is correct here, the different clauses give the impression of wrong parallelism.
Maybe reword: eg

Suggested change
After you complete these steps, you have a working application that
uses the {+php-library+} to connect to your MongoDB deployment, runs a query on
the sample data, and prints out the result.
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
19 changes: 19 additions & 0 deletions source/includes/get-started/quickstart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

use MongoDB\Client;

$client = new Client('<connection string>');
$db = $client->sample_mflix;
$collection = $db->movies;

$filter = ['title' => 'The Shawshank Redemption'];
$result = $collection->findOne($filter);

if ($result) {
echo json_encode($result, JSON_PRETTY_PRINT);
} else {
echo "Document not found";
}
?>
Loading