Skip to content

Commit abd8f5c

Browse files
authored
Merge branch 'php-standardization' into docsp-41960-tls
2 parents b4c8686 + ad5b511 commit abd8f5c

File tree

12 files changed

+927
-6
lines changed

12 files changed

+927
-6
lines changed

snooty.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ php-library = "MongoDB PHP Library"
2626
[constants]
2727

2828
php-library = "MongoDB PHP Library"
29+
driver-short = "PHP library"
2930
mdb-server = "MongoDB Server"
30-
api = "https://www.mongodb.com/docs/php-library/current/reference"
3131
driver-short = "PHP library"
32+
api = "https://www.mongodb.com/docs/php-library/current/reference"

source/connect/client.txt

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
.. _php-client:
2+
3+
=======================
4+
Create a MongoDB Client
5+
=======================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: connection string, URI, server, Atlas, settings
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
To connect to a MongoDB deployment, you must create the following items:
24+
25+
- **Connection URI**, also known as a *connection string*, which tells the {+driver-short+}
26+
which MongoDB deployment to connect to.
27+
- **MongoDB\\Client** object, which creates the connection to the MongoDB deployment
28+
and lets you perform operations on it.
29+
30+
You can also set options within either or both of these components to
31+
customize the way that the {+driver-short+} behaves
32+
while connected to MongoDB.
33+
34+
This guide describes the components of a connection string and shows how to
35+
use a ``MongoDB\Client`` object to connect to a MongoDB deployment.
36+
37+
.. _php-connection-uri:
38+
39+
Connection URI
40+
--------------
41+
42+
A standard connection string includes the following components:
43+
44+
.. list-table::
45+
:widths: 20 80
46+
:header-rows: 1
47+
48+
* - Component
49+
- Description
50+
51+
* - ``mongodb://``
52+
53+
- Required. A prefix that identifies this as a string in the
54+
standard connection format.
55+
56+
* - ``db_username:db_password``
57+
58+
- Optional. Authentication credentials. If you include these, the client
59+
authenticates the user against the database specified in ``authSource``.
60+
For more information about the ``authSource`` connection option, see
61+
:ref:`php-auth`.
62+
63+
* - ``host[:port]``
64+
65+
- Required. The host and optional port number where MongoDB is running. If you don't
66+
include the port number, the driver uses the default port, ``27017``.
67+
68+
* - ``/defaultauthdb``
69+
70+
- Optional. The authentication database to use if the
71+
connection string includes ``db_username:db_password@``
72+
authentication credentials but not the ``authSource`` option. If you don't include
73+
this component, the client authenticates the user against the ``admin`` database.
74+
75+
* - ``?<options>``
76+
77+
- Optional. A query string that specifies connection-specific
78+
options as ``<name>=<value>`` pairs. See
79+
:ref:`php-connection-options` for a full description of
80+
these options.
81+
82+
To learn more about connection strings, see
83+
:manual:`Connection Strings </reference/connection-string>` in the
84+
Server manual.
85+
86+
Create a MongoDB\Client
87+
-----------------------
88+
89+
To create a connection to MongoDB, pass your connection string when constructing
90+
an instance of the ``MongoDB\Client`` class.
91+
92+
In the following example, the library uses a sample connection URI to connect to a MongoDB
93+
deployment on port ``27017`` of ``localhost``:
94+
95+
.. literalinclude:: /includes/connect/client.php
96+
:language: php
97+
:copyable: true
98+
99+
API Documentation
100+
-----------------
101+
102+
To learn more about creating a ``MongoDB\Client`` object in the {+driver-short+},
103+
see the following API documentation:
104+
105+
- :ref:`MongoDB\Client <php-api-mongodbclient>`

source/get-started.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Get Started with the PHP Library
2323
/get-started/download-and-install/
2424
/get-started/create-a-deployment/
2525
/get-started/create-a-connection-string/
26+
/get-started/connect-to-mongodb/
2627
/get-started/next-steps/
2728

2829
Overview
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
.. _php-connect-to-mongodb:
2+
3+
==================
4+
Connect to MongoDB
5+
==================
6+
7+
.. facet::
8+
:name: genre
9+
:values: tutorial
10+
11+
.. meta::
12+
:keywords: test connection, runnable, code example
13+
14+
After retrieving the connection string for your MongoDB Atlas deployment,
15+
you can connect to the deployment from your PHP application and query
16+
the Atlas sample datasets.
17+
18+
.. procedure::
19+
:style: connected
20+
21+
.. step:: Edit your PHP application file
22+
23+
Copy and paste the following code into the ``quickstart.php`` file, which queries
24+
the ``movies`` collection in the ``sample_mflix`` database:
25+
26+
.. literalinclude:: /includes/get-started/quickstart.php
27+
:language: php
28+
:dedent:
29+
30+
.. step:: Assign the connection string
31+
32+
Replace the ``<connection string>`` placeholder with the
33+
connection string that you copied from the :ref:`php-connection-string`
34+
step of this guide.
35+
36+
.. step:: Run your PHP application
37+
38+
In your project directory, run the following shell command to start the application:
39+
40+
.. code-block:: bash
41+
42+
php quickstart.php
43+
44+
The command line output contains details about the retrieved movie
45+
document:
46+
47+
.. code-block:: none
48+
:copyable: false
49+
50+
{
51+
"_id": {
52+
"$oid": "..."
53+
},
54+
...
55+
"rated": "R",
56+
"metacritic": 80,
57+
"title": "The Shawshank Redemption",
58+
...
59+
}
60+
61+
If you encounter an error or see no output, ensure that you specified the
62+
proper connection string in the ``quickstart.php`` file and that you loaded the
63+
sample data.
64+
65+
After you complete these steps, you have a PHP application that
66+
connects to your MongoDB deployment, runs a query on the sample
67+
data, and returns a matching document.
68+
69+
.. include:: /includes/get-started/troubleshoot.rst

source/includes/connect/client.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$client = new MongoDB\Client("mongodb://localhost:27017");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use MongoDB\Client;
6+
7+
$client = new Client('<connection string>');
8+
$collection = $client->sample_mflix->movies;
9+
10+
$filter = ['title' => 'The Shawshank Redemption'];
11+
$result = $collection->findOne($filter);
12+
13+
if ($result) {
14+
echo json_encode($result, JSON_PRETTY_PRINT);
15+
} else {
16+
echo "Document not found";
17+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6+
$client = new MongoDB\Client($uri);
7+
8+
// start-db-coll
9+
$collection = $client->sample_restaurants->restaurants;
10+
// end-db-coll
11+
12+
// Retrieves 5 documents that have a "cuisine" value of "Italian"
13+
// start-limit
14+
$cursor = $collection->find(
15+
['cuisine' => 'Italian'],
16+
['limit' => 5]
17+
);
18+
19+
foreach ($cursor as $doc) {
20+
echo json_encode($doc) . PHP_EOL;
21+
}
22+
// end-limit
23+
24+
// Retrieves documents with a "cuisine" value of "Italian" and sorts in ascending "name" order
25+
// start-sort
26+
$cursor = $collection->find(
27+
['cuisine' => 'Italian'],
28+
['sort' => ['name' => 1]]
29+
);
30+
31+
foreach ($cursor as $doc) {
32+
echo json_encode($doc) . PHP_EOL;
33+
}
34+
// end-sort
35+
36+
// Retrieves documents with a "borough" value of "Manhattan" but skips the first 10 results
37+
// start-skip
38+
$cursor = $collection->find(
39+
['borough' => 'Manhattan'],
40+
['skip' => 10]
41+
);
42+
43+
foreach ($cursor as $doc) {
44+
echo json_encode($doc) . PHP_EOL;
45+
}
46+
// end-skip
47+
48+
// Retrieves 5 documents with a "cuisine" value of "Italian", skips the first 10 results,
49+
// and sorts by ascending "name" order
50+
// start-limit-sort-skip
51+
$options = [
52+
'sort' => ['name' => 1],
53+
'limit' => 5,
54+
'skip' => 10,
55+
];
56+
57+
$cursor = $collection->find(['cuisine' => 'Italian'], $options);
58+
foreach ($cursor as $doc) {
59+
echo json_encode($doc) . PHP_EOL;
60+
}
61+
// end-limit-sort-skip
62+
63+
// Returns documents with a "cuisine" value of "Hawaiian" as arrays
64+
// start-return-type
65+
$options = [
66+
'typeMap' => [
67+
'root' => 'array',
68+
'document' => 'array'
69+
]
70+
];
71+
72+
$cursor = $collection->find(['cuisine' => 'Hawaiian'], $options);
73+
foreach ($cursor as $doc) {
74+
print_r($doc) . PHP_EOL;
75+
}
76+
// end-return-type

0 commit comments

Comments
 (0)