Skip to content

Commit c14727a

Browse files
authored
Merge pull request #148 from lindseymoore/DOCSP-41973
DOCSP-41973 Read Landing Page
2 parents f03fe5b + 63c1926 commit c14727a

File tree

6 files changed

+223
-2
lines changed

6 files changed

+223
-2
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ toc_landing_pages = [
2323
"/reference/class/MongoDBModelIndexInfo",
2424
"/get-started",
2525
"/connect",
26+
"/read",
2627
"/databases-collections",
2728
"/write",
2829
"/indexes",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
require_once __DIR__ . '/vendor/autoload.php';
4+
5+
use MongoDB\Client;
6+
7+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
8+
$client = new MongoDB\Client($uri);
9+
10+
$collection = $client->sample_mflix->movies;
11+
12+
// Find One
13+
// start-find-one
14+
$document = $collection->findOne(['year' => 1994]);
15+
echo json_encode($document) , "\n";
16+
// end-find-one
17+
18+
// Find Multiple
19+
// start-find-multiple
20+
$resultsMultiple = $collection->find(['year' => 1970]);
21+
foreach ($resultsMultiple as $doc) {
22+
echo json_encode($doc) , "\n";
23+
}
24+
// end-find-multiple
25+
26+
// Count Document
27+
// start-count
28+
$result = $collection->countDocuments([]);
29+
echo "Number of documents: " . $result;
30+
// end-count
31+
32+
// Count Specific Documents
33+
// start-count-specific
34+
$result = $collection->countDocuments(['year' => 2010]);
35+
echo "Number of companies founded in 2010: " . $result;
36+
// end-count-specific
37+
38+
// Estimated Count
39+
// start-count-estimate
40+
$result = $collection->estimatedDocumentCount();
41+
echo "Estimated number of documents: " . $result;
42+
// end-count-estimate
43+
44+
// Distinct Values
45+
// start-distinct
46+
$results = $collection->distinct('year');
47+
foreach ($results as $value) {
48+
echo json_encode($value) . PHP_EOL;
49+
}
50+
// end-distinct
51+
52+
// Data Changes
53+
// start-change-stream
54+
$changeStream = $collection->watch();
55+
56+
for ($changeStream->rewind(); true; $changeStream->next()) {
57+
if ( ! $changeStream->valid()) {
58+
continue;
59+
}
60+
$event = $changeStream->current();
61+
echo toJSON($event) . PHP_EOL;
62+
63+
if ($event['operationType'] === 'invalidate') {
64+
break;
65+
}
66+
}
67+
// end-change-stream

source/includes/usage-examples/sample-app-intro.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Sample Application
44
You can use the following sample application to test the code examples on this
55
page. To use the sample application, perform the following steps:
66

7-
1. Ensure you have the {+php-library+} installed in your project.
7+
1. Ensure you have the {+php-library+} installed in your project. To learn more
8+
about installing the {+php-library+}, see the
9+
:ref:`Download and Install <php-download-and-install>` guide.
810
#. Copy the following code and paste it into a new ``.php`` file.
911
#. Copy a code example from this page and paste it on the specified
1012
lines in the file.

source/read.txt

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
Read Data from MongoDB
55
======================
66

7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:description: Learn how to use the PHP Library to read data to MongoDB.
19+
:keywords: usage examples, save, crud, read, code example
20+
721
.. toctree::
822
:titlesonly:
923
:maxdepth: 1
@@ -16,3 +30,136 @@ Read Data from MongoDB
1630
/read/specify-a-query
1731
/read/distinct
1832
/read/change-streams
33+
34+
Overview
35+
--------
36+
37+
On this page, you can see copyable code examples that show common
38+
{+driver-short+} methods for retrieving documents.
39+
40+
.. tip::
41+
42+
To learn more about any of the methods shown on this page, see the link
43+
provided in each section.
44+
45+
To use an example from this page, copy the code example into the
46+
:ref:`sample application <php-index-sample>` or your own application.
47+
Make sure to set the ``MONGODB_URI`` environment variable to the
48+
connection string for your MongoDB deployment, and replace the
49+
``<database>`` and ``<collection>`` placeholders with values for your
50+
target namespace.
51+
52+
.. _php-read-sample:
53+
54+
.. include:: /includes/usage-examples/sample-app-intro.rst
55+
56+
.. literalinclude:: /includes/usage-examples/sample-app.php
57+
:language: php
58+
:dedent:
59+
:linenos:
60+
:emphasize-lines: 10-12
61+
62+
Find One
63+
--------
64+
65+
The following code shows how to retrieve a single document from a collection
66+
that matches the specified criteria:
67+
68+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
69+
:start-after: start-find-one
70+
:end-before: end-find-one
71+
:language: php
72+
:dedent:
73+
74+
To learn more about the ``findOne()`` method, see the :ref:`php-retrieve-find-one`
75+
section in the Retrieve Data guide.
76+
77+
Find Multiple
78+
-------------
79+
80+
The following code shows how to retrieve all documents from a collection
81+
that match the specified criteria:
82+
83+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
84+
:start-after: start-find-multiple
85+
:end-before: end-find-multiple
86+
:language: php
87+
:dedent:
88+
89+
To learn more about the ``find()`` method, see the :ref:`php-retrieve-find-multiple`
90+
section in the Retrieve Data guide.
91+
92+
Count Documents in a Collection
93+
-------------------------------
94+
95+
The following code shows how to count the number of documents in
96+
a collection:
97+
98+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
99+
:start-after: start-count
100+
:end-before: end-count
101+
:language: php
102+
:dedent:
103+
104+
To learn more about the ``countDocuments()`` method, see the
105+
:ref:`php-count-all` section in the Count Documents guide.
106+
107+
Count Documents Returned from a Query
108+
-------------------------------------
109+
110+
The following code shows how to count documents in a collection
111+
that match the specified criteria:
112+
113+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
114+
:start-after: start-count-specific
115+
:end-before: end-count-specific
116+
:language: php
117+
:dedent:
118+
119+
To learn more about the ``countDocuments()`` method, see the
120+
:ref:`php-count-specific` section in the Count Documents guide.
121+
122+
Estimated Document Count
123+
------------------------
124+
125+
The following code shows how to retrieve an estimated count of the
126+
number of documents in a collection:
127+
128+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
129+
:start-after: start-count-estimate
130+
:end-before: end-count-estimate
131+
:language: php
132+
:dedent:
133+
134+
To learn more about the ``estimatedDocumentCount()`` method, see the
135+
:ref:`php-estimated-count` section in the Count Documents guide.
136+
137+
Retrieve Distinct Values
138+
------------------------
139+
140+
The following code shows how to retrieve the unique values of a field
141+
for documents that match the specified criteria:
142+
143+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
144+
:start-after: start-distinct
145+
:end-before: end-distinct
146+
:language: php
147+
:dedent:
148+
149+
To learn more about the ``distinct()`` method, see the
150+
:ref:`php-distinct` guide.
151+
152+
Monitor Data Changes
153+
--------------------
154+
155+
The following code shows how to monitor and print changes to a
156+
collection:
157+
158+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
159+
:start-after: start-change-stream
160+
:end-before: end-change-stream
161+
:language: php
162+
:dedent:
163+
164+
To learn more about the ``watch()`` method, see the
165+
:ref:`php-change-streams` guide.

source/read/count.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pass a query filter to the ``countDocuments()`` method.
5858

5959
To learn more about specifying a query, see the :ref:`php-specify-query` guide.
6060

61+
.. _php-count-all:
62+
6163
Count All Documents
6264
~~~~~~~~~~~~~~~~~~~
6365

@@ -78,6 +80,8 @@ the ``countDocuments()`` method, as shown in the following example:
7880

7981
Number of documents: 9500
8082

83+
.. _php-count-specific:
84+
8185
Count Specific Documents
8286
~~~~~~~~~~~~~~~~~~~~~~~~
8387

source/read/retrieve.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The {+php-library+} includes two methods for retrieving documents from a collect
5252
take a **query filter** and return one or more matching documents. A query filter
5353
specifies the search criteria that the driver uses to retrieve documents in your query.
5454

55-
.. TODO: To learn more about query filters, see :ref:`php-specify-query`.
55+
To learn more about query filters, see :ref:`php-specify-query`.
5656

5757
.. _php-retrieve-find-one:
5858

0 commit comments

Comments
 (0)