From cfb9d3166f9bde019c66e50482f74cae84302a28 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 8 Aug 2024 11:52:29 -0400 Subject: [PATCH 1/6] DOCSP-41955: Connect to MongoDB --- source/get-started.txt | 3 +- source/get-started/connect-to-mongodb.txt | 63 ++++++++++++++++++++++ source/includes/get-started/quickstart.php | 19 +++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 source/get-started/connect-to-mongodb.txt create mode 100644 source/includes/get-started/quickstart.php diff --git a/source/get-started.txt b/source/get-started.txt index edd20aef..236ffe27 100644 --- a/source/get-started.txt +++ b/source/get-started.txt @@ -20,4 +20,5 @@ Get Started with the PHP Library .. toctree:: - /get-started/create-a-deployment/ \ No newline at end of file + /get-started/create-a-deployment/ + /get-started/connect-to-mongodb/ \ No newline at end of file diff --git a/source/get-started/connect-to-mongodb.txt b/source/get-started/connect-to-mongodb.txt new file mode 100644 index 00000000..19911230 --- /dev/null +++ b/source/get-started/connect-to-mongodb.txt @@ -0,0 +1,63 @@ +.. _php-connect-to-mongodb: + +================== +Connect to MongoDB +================== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: test connection, runnable, code example + +.. 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 + + .. step:: Assign the connection string + + Replace the ```` 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 run 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.cpp`` 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. + +.. include:: /includes/get-started/troubleshoot.rst diff --git a/source/includes/get-started/quickstart.php b/source/includes/get-started/quickstart.php new file mode 100644 index 00000000..ecdb1ef1 --- /dev/null +++ b/source/includes/get-started/quickstart.php @@ -0,0 +1,19 @@ +'); +$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"; +} +?> \ No newline at end of file From 13ea6745f1c548595238b26e00bbbc6c0423000d Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 8 Aug 2024 11:55:22 -0400 Subject: [PATCH 2/6] edits --- source/get-started/connect-to-mongodb.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/get-started/connect-to-mongodb.txt b/source/get-started/connect-to-mongodb.txt index 19911230..b4a31aee 100644 --- a/source/get-started/connect-to-mongodb.txt +++ b/source/get-started/connect-to-mongodb.txt @@ -20,6 +20,8 @@ Connect to MongoDB the ``movies`` collection in the ``sample_mflix`` database: .. literalinclude:: /includes/get-started/quickstart.php + :language: php + :dedent: .. step:: Assign the connection string @@ -29,7 +31,7 @@ Connect to MongoDB .. step:: Run your PHP application - In your project directory, run the following shell command to run the application: + In your project directory, run the following shell command to start the application: .. code-block:: bash @@ -53,7 +55,7 @@ Connect to MongoDB } If you encounter an error or see no output, ensure that you specified the - proper connection string in the ``quickstart.cpp`` file and that you loaded 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 From a69a8052f845e8a0651831ec6dea7a7216895ee0 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 8 Aug 2024 13:37:51 -0400 Subject: [PATCH 3/6] edit --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index b42a4f24..85d69aa9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ build/* build *~ *giza.log -source/* backups/* From 6a1cd308337cb2a47c6aefb05edb050e0cca1ff8 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 8 Aug 2024 15:33:03 -0400 Subject: [PATCH 4/6] RR feedback --- source/get-started/connect-to-mongodb.txt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/source/get-started/connect-to-mongodb.txt b/source/get-started/connect-to-mongodb.txt index b4a31aee..3b9bef13 100644 --- a/source/get-started/connect-to-mongodb.txt +++ b/source/get-started/connect-to-mongodb.txt @@ -11,6 +11,10 @@ Connect to MongoDB .. meta:: :keywords: test connection, runnable, code example +After retrieving the connection string for your MongoDB Atlas deployment, +you can connection to the deployment from your PHP application and query +the Atlas sample datasets. + .. procedure:: :style: connected @@ -58,8 +62,8 @@ Connect to MongoDB 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. +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 From bd636abeb4205aacd6272945a9ead52db8f5d31e Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 8 Aug 2024 15:36:30 -0400 Subject: [PATCH 5/6] typo --- source/get-started/connect-to-mongodb.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/get-started/connect-to-mongodb.txt b/source/get-started/connect-to-mongodb.txt index 3b9bef13..7492fe25 100644 --- a/source/get-started/connect-to-mongodb.txt +++ b/source/get-started/connect-to-mongodb.txt @@ -12,7 +12,7 @@ Connect to MongoDB :keywords: test connection, runnable, code example After retrieving the connection string for your MongoDB Atlas deployment, -you can connection to the deployment from your PHP application and query +you can connect to the deployment from your PHP application and query the Atlas sample datasets. .. procedure:: From ca2f63467ea5f92a7407be8fc96d3f2cd8ed7ccc Mon Sep 17 00:00:00 2001 From: norareidy Date: Mon, 19 Aug 2024 17:19:45 -0400 Subject: [PATCH 6/6] code edit --- source/includes/get-started/quickstart.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/includes/get-started/quickstart.php b/source/includes/get-started/quickstart.php index ecdb1ef1..d62dd83e 100644 --- a/source/includes/get-started/quickstart.php +++ b/source/includes/get-started/quickstart.php @@ -5,8 +5,7 @@ use MongoDB\Client; $client = new Client(''); -$db = $client->sample_mflix; -$collection = $db->movies; +$collection = $client->sample_mflix->movies; $filter = ['title' => 'The Shawshank Redemption']; $result = $collection->findOne($filter); @@ -16,4 +15,3 @@ } else { echo "Document not found"; } -?> \ No newline at end of file