4
4
Read Data from MongoDB
5
5
======================
6
6
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
+
7
21
.. toctree::
8
22
:titlesonly:
9
23
:maxdepth: 1
@@ -16,3 +30,136 @@ Read Data from MongoDB
16
30
/read/specify-a-query
17
31
/read/distinct
18
32
/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.
0 commit comments