|
| 1 | +.. _ruby-retrieve: |
| 2 | + |
| 3 | +============= |
| 4 | +Retrieve Data |
| 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 | + :keywords: code examples, read, query, cursor |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+driver-short+} to retrieve |
| 24 | +data from a MongoDB collection by using **read operations**. You can call the |
| 25 | +``find`` method on a collection to retrieve documents that match a set of |
| 26 | +criteria. |
| 27 | + |
| 28 | +Sample Data |
| 29 | +~~~~~~~~~~~ |
| 30 | + |
| 31 | +The examples in this guide use the ``companies`` collection in the ``sample_training`` |
| 32 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection |
| 33 | +from your {+language+} application, create a ``Mongo::Client`` object that connects to |
| 34 | +an Atlas cluster and assign the following values to your ``database`` and ``collection`` |
| 35 | +variables: |
| 36 | + |
| 37 | +.. literalinclude:: /includes/read/retrieve.rb |
| 38 | + :language: ruby |
| 39 | + :dedent: |
| 40 | + :start-after: start-db-coll |
| 41 | + :end-before: end-db-coll |
| 42 | + |
| 43 | +To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 44 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 45 | + |
| 46 | +.. _ruby-retrieve-find: |
| 47 | + |
| 48 | +Find Documents |
| 49 | +-------------- |
| 50 | + |
| 51 | +To retrieve documents from a collection, use the ``find`` method. This method |
| 52 | +takes a **query filter** parameter and returns a ``Mongo::Collection::View`` object, |
| 53 | +which represents the query. The driver defers the query execution until you |
| 54 | +fetch the results by using methods like ``first`` or ``each``. After you request |
| 55 | +the results, the driver sends the query to the server and returns a ``Mongo::Cursor`` |
| 56 | +object from which you can access the results. |
| 57 | + |
| 58 | +You can chain option methods to the ``find`` method to refine the results of the operation. |
| 59 | + |
| 60 | +.. TODO: To learn more about query filters, see the :ref:`ruby-specify-query` guide. |
| 61 | + |
| 62 | +.. _ruby-retrieve-find-multiple: |
| 63 | + |
| 64 | +Find Multiple Documents |
| 65 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 66 | + |
| 67 | +To find multiple documents in a collection, pass a query filter to the |
| 68 | +``find`` method that specifies the criteria of the documents you want to retrieve. |
| 69 | + |
| 70 | +The following example uses the ``find`` method to find all documents in which |
| 71 | +the ``founded_year`` field has the value ``1970``: |
| 72 | + |
| 73 | +.. literalinclude:: /includes/read/retrieve.rb |
| 74 | + :language: ruby |
| 75 | + :dedent: |
| 76 | + :start-after: start-find-many |
| 77 | + :end-before: end-find-many |
| 78 | + |
| 79 | +When you call the ``each`` method on the ``Mongo::Collection::View`` object representing |
| 80 | +the query, the driver returns a ``Mongo::Cursor`` object. A cursor is a mechanism that allows an |
| 81 | +application to iterate over database results while holding only a subset of them in |
| 82 | +memory at a given time. Cursors are useful when your ``find`` method returns a large |
| 83 | +amount of documents. |
| 84 | + |
| 85 | +The following code calls the ``each`` method to iterate over the query results: |
| 86 | + |
| 87 | +.. io-code-block:: |
| 88 | + :copyable: |
| 89 | + |
| 90 | + .. input:: /includes/read/retrieve.rb |
| 91 | + :start-after: start-cursor |
| 92 | + :end-before: end-cursor |
| 93 | + :language: ruby |
| 94 | + :dedent: |
| 95 | + |
| 96 | + .. output:: |
| 97 | + :visible: false |
| 98 | + |
| 99 | + {"_id"=>BSON::ObjectId('...'), "name"=>"Mitsubishi Motors", "permalink"=>"mitsubishi-motors", |
| 100 | + "crunchbase_url"=>"http://www.crunchbase.com/company/mitsubishi-motors", |
| 101 | + "homepage_url"=>"http://www.mitsubishi-motors.com", ...} |
| 102 | + {"_id"=>BSON::ObjectId('...'), "name"=>"Western Digital", "permalink"=>"western-digital", |
| 103 | + "crunchbase_url"=>"http://www.crunchbase.com/company/western-digital", |
| 104 | + "homepage_url"=>"http://www.wdc.com/en", ...} |
| 105 | + {"_id"=>BSON::ObjectId('...'), "name"=>"Celarayn", "permalink"=>"celarayn", |
| 106 | + "crunchbase_url"=>"http://www.crunchbase.com/company/celarayn", |
| 107 | + "homepage_url"=>"http://www.celarayn.es", ...} |
| 108 | + |
| 109 | +.. note:: Find All Documents |
| 110 | + |
| 111 | + To find all documents in a collection, call the ``find`` method |
| 112 | + without passing a query filter: |
| 113 | + |
| 114 | + .. code-block:: ruby |
| 115 | + |
| 116 | + results = collection.find |
| 117 | + |
| 118 | +.. _ruby-retrieve-find-one: |
| 119 | + |
| 120 | +Find One Document |
| 121 | +~~~~~~~~~~~~~~~~~ |
| 122 | + |
| 123 | +To find a single document in a collection, call the ``find`` method and pass a query |
| 124 | +filter that specifies the criteria of the document you want to find. Then, chain |
| 125 | +the ``first`` method to ``find``. |
| 126 | + |
| 127 | +If the query filter matches more than one document, the ``first`` method retrieves the *first* |
| 128 | +matching document from the operation results. |
| 129 | + |
| 130 | +The following example chains the ``first`` method to ``find`` to find the first |
| 131 | +document in which the ``name`` field has the value ``'LinkedIn'``: |
| 132 | + |
| 133 | +.. io-code-block:: |
| 134 | + :copyable: |
| 135 | + |
| 136 | + .. input:: /includes/read/retrieve.rb |
| 137 | + :start-after: start-find-one |
| 138 | + :end-before: end-find-one |
| 139 | + :language: ruby |
| 140 | + :dedent: |
| 141 | + |
| 142 | + .. output:: |
| 143 | + :visible: false |
| 144 | + |
| 145 | + {"_id"=>BSON::ObjectId('...'), "name"=>"LinkedIn", "permalink"=>"linkedin", |
| 146 | + "crunchbase_url"=>"http://www.crunchbase.com/company/linkedin", |
| 147 | + "homepage_url"=>"http://linkedin.com", "blog_url"=>"http://blog.linkedin.com", |
| 148 | + ...} |
| 149 | + |
| 150 | +.. tip:: Sort Order |
| 151 | + |
| 152 | + The ``first`` method returns the first document in |
| 153 | + :manual:`natural order </reference/glossary/#std-term-natural-order>` |
| 154 | + on disk if no sort criteria is specified. |
| 155 | + |
| 156 | +.. _ruby-retrieve-modify: |
| 157 | + |
| 158 | +Modify Find Behavior |
| 159 | +~~~~~~~~~~~~~~~~~~~~ |
| 160 | + |
| 161 | +You can chain option methods to the ``find`` method to modify the operation |
| 162 | +results. The following table describes some of these options: |
| 163 | + |
| 164 | +.. list-table:: |
| 165 | + :widths: 30 70 |
| 166 | + :header-rows: 1 |
| 167 | + |
| 168 | + * - Option |
| 169 | + - Description |
| 170 | + |
| 171 | + * - ``batch_size`` |
| 172 | + - | The number of documents to return per batch. The default value is ``101``. |
| 173 | + | **Type**: ``Integer`` |
| 174 | + |
| 175 | + * - ``collation`` |
| 176 | + - | The collation to use for the operation. The default value is the collation |
| 177 | + specified for the collection. |
| 178 | + | **Type**: ``Hash`` |
| 179 | + |
| 180 | + * - ``comment`` |
| 181 | + - | The comment to attach to the operation. |
| 182 | + | **Type**: ``Object`` |
| 183 | + |
| 184 | + * - ``limit`` |
| 185 | + - | The maximum number of documents the operation can return. |
| 186 | + | **Type**: ``Integer`` |
| 187 | + |
| 188 | + * - ``skip`` |
| 189 | + - | The number of documents to skip before returning results. |
| 190 | + | **Type**: ``Integer`` |
| 191 | + |
| 192 | + * - ``sort`` |
| 193 | + - | The order in which the operation returns matching documents. |
| 194 | + | **Type**: ``Hash`` |
| 195 | + |
| 196 | +The following example uses the ``find`` method to find all documents in which |
| 197 | +the ``number_of_employees`` field has the value ``1000``. The example uses the |
| 198 | +``limit`` option to return a maximum of ``2`` results: |
| 199 | + |
| 200 | +.. io-code-block:: |
| 201 | + :copyable: |
| 202 | + |
| 203 | + .. input:: /includes/read/retrieve.rb |
| 204 | + :start-after: start-modify |
| 205 | + :end-before: end-modify |
| 206 | + :language: ruby |
| 207 | + :dedent: |
| 208 | + |
| 209 | + .. output:: |
| 210 | + :visible: false |
| 211 | + |
| 212 | + {"_id"=>BSON::ObjectId('...'), "name"=>"Akamai Technologies", "permalink"=>"akamai-technologies", |
| 213 | + "crunchbase_url"=>"http://www.crunchbase.com/company/akamai-technologies", |
| 214 | + "homepage_url"=>"http://www.akamai.com", ...} |
| 215 | + {"_id"=>BSON::ObjectId('...'), "name"=>"Yodle", "permalink"=>"yodle", |
| 216 | + "crunchbase_url"=>"http://www.crunchbase.com/company/yodle", |
| 217 | + "homepage_url"=>"http://www.yodle.com", ...} |
| 218 | + |
| 219 | +For a full list of options, see the API documentation for the |
| 220 | +`find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__ |
| 221 | +method. |
| 222 | + |
| 223 | +.. _ruby-retrieve-additional-information: |
| 224 | + |
| 225 | +Additional Information |
| 226 | +---------------------- |
| 227 | + |
| 228 | +.. TODO: |
| 229 | + To learn more about query filters, see the :ref:`ruby-specify-query` guide. |
| 230 | + To view code examples of retrieving documents with the {+driver-short+}, see :ref:`ruby-read`. |
| 231 | + |
| 232 | +API Documentation |
| 233 | +~~~~~~~~~~~~~~~~~ |
| 234 | + |
| 235 | +To learn more about any of the methods or types discussed in this |
| 236 | +guide, see the following API documentation: |
| 237 | + |
| 238 | +- `find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__ |
| 239 | +- `Mongo::Collection::View <{+api-root+}/Mongo/Collection/View.html>`__ |
| 240 | +- `Mongo::Cursor <{+api-root+}/Mongo/Cursor.html>`__ |
0 commit comments