Skip to content

Commit f11a302

Browse files
committed
full page with ex
1 parent a5f2990 commit f11a302

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed
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/read.txt

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

7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
713
.. toctree::
814
:titlesonly:
915
:maxdepth: 1
@@ -35,6 +41,115 @@ the relevant values for your MongoDB deployment.
3541

3642
.. _php-read-sample:
3743

44+
.. include:: /includes/usage-examples/sample-app-intro.rst
45+
46+
.. literalinclude:: /includes/usage-examples/sample-app.php
47+
:language: php
48+
:dedent:
49+
:linenos:
50+
:emphasize-lines: 10-12
51+
52+
Find One
53+
--------
54+
55+
The following code shows how to retrieve a single document from a collection
56+
that matches the specified criteria:
57+
58+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
59+
:start-after: start-find-one
60+
:end-before: end-find-one
61+
:language: php
62+
:dedent:
63+
64+
To learn more about the ``findOne()`` method, see the :ref:`php-retrieve-find-one`
65+
section in the Retrieve Data guide.
66+
67+
Find Multiple
68+
-------------
69+
70+
The following code shows how to retrieve all documents from a collection
71+
that match the specified criteria:
72+
73+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
74+
:start-after: start-find-multiple
75+
:end-before: end-find-multiple
76+
:language: php
77+
:dedent:
78+
79+
To learn more about the ``find()`` method, see the :ref:`php-retrieve-find-multiple`
80+
section in the Retrieve Data guide.
81+
82+
Count Documents in a Collection
83+
-------------------------------
84+
85+
The following code shows how to count the number of documents in
86+
a collection:
87+
88+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
89+
:start-after: start-count
90+
:end-before: end-count
91+
:language: php
92+
:dedent:
93+
94+
To learn more about the ``countDocuments()`` method, see the
95+
:ref:`php-accurate-count` section in the Count Documents guide.
96+
97+
Count Documents Returned from a Query
98+
-------------------------------------
99+
100+
The following code shows how to count documents in a collection
101+
that match the specified criteria:
102+
103+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
104+
:start-after: start-count-specific
105+
:end-before: end-count-specific
106+
:language: php
107+
:dedent:
108+
109+
To learn more about the ``countDocuments()`` method, see the
110+
:ref:`php-accurate-count` section in the Count Documents guide.
111+
112+
Estimated Document Count
113+
------------------------
114+
115+
The following code shows how to retrieve an estimated count of the
116+
number of documents in a collection:
117+
118+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
119+
:start-after: start-count-estimate
120+
:end-before: end-count-estimate
121+
:language: php
122+
:dedent:
123+
124+
To learn more about the ``estimatedDocumentCount()`` method, see the
125+
:ref:`php-estimated-count` section in the Count Documents guide.
126+
127+
Retrieve Distinct Values
128+
------------------------
129+
130+
The following code shows how to retrieve the unique values of a field
131+
for documents that match the specified criteria:
132+
133+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
134+
:start-after: start-distinct
135+
:end-before: end-distinct
136+
:language: php
137+
:dedent:
138+
139+
To learn more about the ``distinct()`` method, see the
140+
:ref:`php-distinct` guide.
141+
142+
Monitor Data Changes
143+
--------------------
38144

145+
The following code shows how to monitor and print changes to a
146+
collection:
39147

148+
.. literalinclude:: /includes/usage-examples/read-code-examples.php
149+
:start-after: start-change-stream
150+
:end-before: end-change-stream
151+
:language: php
152+
:dedent:
40153

154+
To learn more about the ``watch()`` method, see the
155+
:ref:`php-change-streams` guide.

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)