|
| 1 | +.. _kotlin-sync-read: |
| 2 | + |
| 3 | +====================== |
| 4 | +Read Data from MongoDB |
| 5 | +====================== |
| 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 Kotlin Sync Driver to read data from MongoDB. |
| 19 | + :keywords: usage examples, query, find, code example |
| 20 | + |
| 21 | +.. .. toctree:: |
| 22 | +.. :titlesonly: |
| 23 | +.. :maxdepth: 1 |
| 24 | + |
| 25 | + .. /read/specify-a-query |
| 26 | + .. /read/retrieve |
| 27 | + .. /read/project |
| 28 | + .. /read/specify-documents-to-return |
| 29 | + .. /read/count |
| 30 | + .. /read/distinct |
| 31 | + .. /read/cursors |
| 32 | + .. /read/change-streams |
| 33 | + |
| 34 | +Overview |
| 35 | +-------- |
| 36 | + |
| 37 | +On this page, you can see copyable code examples that show common |
| 38 | +methods you can use to retrieve documents by using the {+driver-short+}. |
| 39 | + |
| 40 | +.. tip:: |
| 41 | + |
| 42 | + To learn more about any of the methods shown on this page, see the link |
| 43 | + to a relevant guide provided in each section. |
| 44 | + |
| 45 | +To use an example from this page, copy the code example into the |
| 46 | +:ref:`sample application <kotlin-sync-read-sample>` or your own application. |
| 47 | +Be sure to replace all placeholders in the code examples, such as ``<connection string URI>``, with |
| 48 | +the relevant values for your MongoDB deployment. |
| 49 | + |
| 50 | +.. _kotlin-sync-read-sample: |
| 51 | + |
| 52 | +.. include:: /includes/usage-examples/sample-app-intro.rst |
| 53 | + |
| 54 | +.. literalinclude:: /includes/usage-examples/sample-app.kt |
| 55 | + :language: kotlin |
| 56 | + :copyable: |
| 57 | + :linenos: |
| 58 | + :emphasize-lines: 20-22 |
| 59 | + |
| 60 | +.. .. tip:: |
| 61 | + |
| 62 | +.. For instructions about how to install the {+driver-short+}, see :ref:`<kotlin-sync-quick-start>`. |
| 63 | + |
| 64 | +Find Documents |
| 65 | +-------------- |
| 66 | + |
| 67 | +The following example retrieves a list of documents that match the criteria specified by the |
| 68 | +given filter: |
| 69 | + |
| 70 | +.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt |
| 71 | + :start-after: start-find |
| 72 | + :end-before: end-find |
| 73 | + :language: kotlin |
| 74 | + :copyable: |
| 75 | + :dedent: |
| 76 | + |
| 77 | +.. TODO: To learn more about the ``find()`` method, see the :ref:`kotlin-sync-retrieve-find` guide. |
| 78 | + |
| 79 | +Count Documents in a Collection |
| 80 | +------------------------------- |
| 81 | + |
| 82 | +The following example returns the number of documents in the specified collection: |
| 83 | + |
| 84 | +.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt |
| 85 | + :start-after: start-count-all |
| 86 | + :end-before: end-count-all |
| 87 | + :language: kotlin |
| 88 | + :copyable: |
| 89 | + :dedent: |
| 90 | + |
| 91 | +.. TODO: To learn more about the ``count_documents()`` method, see the |
| 92 | +.. :ref:`kotlin-sync-accurate-count` guide. |
| 93 | + |
| 94 | +Count Documents Returned from a Query |
| 95 | +------------------------------------- |
| 96 | + |
| 97 | +The following example returns the number of documents that match the criteria specified by |
| 98 | +the given filter: |
| 99 | + |
| 100 | +.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt |
| 101 | + :start-after: start-count-query |
| 102 | + :end-before: end-count-query |
| 103 | + :language: kotlin |
| 104 | + :copyable: |
| 105 | + :dedent: |
| 106 | + |
| 107 | +.. TODO: To learn more about the ``countDocuments()`` method, see the |
| 108 | +.. :ref:`kotlin-sync-accurate-count` guide. |
| 109 | + |
| 110 | +Estimated Document Count |
| 111 | +------------------------ |
| 112 | + |
| 113 | +The following example returns an approximate number of documents in the specified |
| 114 | +collection based on collection metadata: |
| 115 | + |
| 116 | +.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt |
| 117 | + :start-after: start-estimated-count |
| 118 | + :end-before: end-estimated-count |
| 119 | + :language: kotlin |
| 120 | + :copyable: |
| 121 | + :dedent: |
| 122 | + |
| 123 | +.. TODO: To learn more about the ``estimatedDocumentCount()`` method, see the |
| 124 | +.. :ref:`kotlin-sync-estimated-count` guide. |
| 125 | + |
| 126 | +Retrieve Distinct Values |
| 127 | +------------------------ |
| 128 | + |
| 129 | +The following example returns all distinct values of the specified field name in a given |
| 130 | +collection: |
| 131 | + |
| 132 | +.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt |
| 133 | + :start-after: start-distinct |
| 134 | + :end-before: end-distinct |
| 135 | + :language: kotlin |
| 136 | + :copyable: |
| 137 | + :dedent: |
| 138 | + |
| 139 | +.. TODO: To learn more about the ``distinct()`` method, see the |
| 140 | +.. :ref:`kotlin-sync-distinct` guide. |
| 141 | + |
| 142 | +Monitor Data Changes |
| 143 | +-------------------- |
| 144 | + |
| 145 | +The following example creates a change stream for a given collection and prints out |
| 146 | +subsequent change events in that collection: |
| 147 | + |
| 148 | +.. literalinclude:: /includes/usage-examples/retrieve-code-examples.kt |
| 149 | + :start-after: start-watch |
| 150 | + :end-before: end-watch |
| 151 | + :language: kotlin |
| 152 | + :copyable: |
| 153 | + :dedent: |
| 154 | + |
| 155 | +.. TODO: To learn more about the ``watch()`` method, see the |
| 156 | +.. :ref:`kotlin-sync-change-streams` guide. |
0 commit comments