Skip to content

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
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions source/get-started.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Get Started with the PHP Library
/get-started/download-and-install/
/get-started/create-a-deployment/
/get-started/create-a-connection-string/
/get-started/connect-to-mongodb/
/get-started/next-steps/

Overview
Expand Down
69 changes: 69 additions & 0 deletions source/get-started/connect-to-mongodb.txt
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

Copy link
Collaborator

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 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
17 changes: 17 additions & 0 deletions source/includes/get-started/quickstart.php
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";
}
Loading