Skip to content

Commit ec622a7

Browse files
committed
Merge remote-tracking branch 'upstream/php-standardization' into DOCSP-41977-docs-to-return
2 parents 8bab89e + cbead21 commit ec622a7

File tree

6 files changed

+463
-3
lines changed

6 files changed

+463
-3
lines changed

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
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: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
use MongoDB\Client;
6+
7+
// start-setup
8+
$uri = "<connection string>";
9+
$client = new Client($uri);
10+
$collection = $client->db->fruits;
11+
12+
// Inserts documents representing fruits
13+
$fruits = [
14+
[
15+
'_id' => 1,
16+
'name' => 'apples',
17+
'qty' => 5,
18+
'rating' => 3,
19+
'color' => 'red',
20+
'type' => ['fuji', 'honeycrisp']
21+
],
22+
[
23+
'_id' => 2,
24+
'name' => 'bananas',
25+
'qty' => 7,
26+
'rating' => 4,
27+
'color' => 'yellow',
28+
'type' => ['cavendish']
29+
],
30+
[
31+
'_id' => 3,
32+
'name' => 'oranges',
33+
'qty' => 6,
34+
'rating' => 2,
35+
'type' => ['naval', 'mandarin']
36+
],
37+
[
38+
'_id' => 4,
39+
'name' => 'pineapples',
40+
'qty' => 3,
41+
'rating' => 5,
42+
'color' => 'yellow'
43+
]
44+
];
45+
46+
$result = $collection->insertMany($fruits);
47+
// end-setup
48+
49+
// Retrieves documents in which the "color" value is "yellow"
50+
// start-find-exact
51+
$cursor = $collection->find(['color' => 'yellow']);
52+
foreach ($cursor as $doc) {
53+
echo json_encode($doc) . PHP_EOL;
54+
}
55+
// end-find-exact
56+
57+
// Retrieves all documents in the collection
58+
// start-find-all
59+
$cursor = $collection->find([]);
60+
foreach ($cursor as $doc) {
61+
echo json_encode($doc) . PHP_EOL;
62+
}
63+
// end-find-all
64+
65+
// Retrieves and prints documents in which the "rating" value is greater than 2
66+
// start-find-comparison
67+
$cursor = $collection->find(['rating' => ['$gt' => 2]]);
68+
foreach ($cursor as $doc) {
69+
echo json_encode($doc) . PHP_EOL;
70+
}
71+
// end-find-comparison
72+
73+
// Retrieves and prints documents that match one or both query filters
74+
// start-find-logical
75+
$cursor = $collection->find([
76+
'$or' => [
77+
['qty' => ['$gt' => 5]],
78+
['color' => 'yellow']
79+
]
80+
]);
81+
foreach ($cursor as $doc) {
82+
echo json_encode($doc) . PHP_EOL;
83+
}
84+
// end-find-logical
85+
86+
// Retrieves and prints documents in which the "type" array has 2 elements
87+
// start-find-array
88+
$cursor = $collection->find(['type' => ['$size' => 2]]);
89+
foreach ($cursor as $doc) {
90+
echo json_encode($doc) . PHP_EOL;
91+
}
92+
// end-find-array
93+
94+
// Retrieves and prints documents that have a "color" field
95+
// start-find-element
96+
$cursor = $collection->find(['color' => ['$exists' => true]]);
97+
foreach ($cursor as $doc) {
98+
echo json_encode($doc) . PHP_EOL;
99+
}
100+
// end-find-element
101+
102+
// Retrieves and prints documents in which the "name" value has at least two consecutive "p" characters
103+
// start-find-evaluation
104+
$cursor = $collection->find(['name' => ['$regex' => 'p{2,}']]);
105+
foreach ($cursor as $doc) {
106+
echo json_encode($doc) . PHP_EOL;
107+
}
108+
// end-find-evaluation

source/read.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Read Data from MongoDB
77
.. toctree::
88
:titlesonly:
99
:maxdepth: 1
10-
10+
1111
/read/retrieve
1212
/read/specify-documents-to-return
13-
/read/project
14-
13+
/read/specify-a-query
14+
/read/project

0 commit comments

Comments
 (0)