|
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 | + |
7 | 13 | .. toctree::
|
8 | 14 | :titlesonly:
|
9 | 15 | :maxdepth: 1
|
@@ -35,6 +41,115 @@ the relevant values for your MongoDB deployment.
|
35 | 41 |
|
36 | 42 | .. _php-read-sample:
|
37 | 43 |
|
| 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 | +-------------------- |
38 | 144 |
|
| 145 | +The following code shows how to monitor and print changes to a |
| 146 | +collection: |
39 | 147 |
|
| 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: |
40 | 153 |
|
| 154 | +To learn more about the ``watch()`` method, see the |
| 155 | +:ref:`php-change-streams` guide. |
0 commit comments