|
| 1 | +.. _ruby-get-started-connect-to-mongodb: |
| 2 | + |
| 3 | +================== |
| 4 | +Connect to MongoDB |
| 5 | +================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: test connection, runnable, code example |
| 13 | + |
| 14 | +.. procedure:: |
| 15 | + :style: connected |
| 16 | + |
| 17 | + .. step:: Edit your {+language+} application file |
| 18 | + |
| 19 | + Navigate to your ``quickstart.rb`` file in the ``ruby-quickstart`` |
| 20 | + directory. Copy and paste the following code below the Bundler |
| 21 | + code from the :ref:`ruby-quick-start-download-and-install` step |
| 22 | + of this tutorial. This code connects to MongoDB and queries the |
| 23 | + ``movies`` collection in the ``sample_mflix`` database. |
| 24 | + |
| 25 | + .. literalinclude:: /includes/get-started/quickstart.rb |
| 26 | + :language: ruby |
| 27 | + :dedent: |
| 28 | + :start-after: start-query |
| 29 | + :end-before: end-query |
| 30 | + |
| 31 | + .. step:: Assign the connection string |
| 32 | + |
| 33 | + Replace the ``<connection string>`` placeholder with the |
| 34 | + connection string that you copied from the :ref:`ruby-get-started-connection-string` |
| 35 | + step of this tutorial. |
| 36 | + |
| 37 | + .. step:: Run your {+language+} application |
| 38 | + |
| 39 | + From your ``ruby-quickstart`` directory, run the following shell |
| 40 | + command to run the application: |
| 41 | + |
| 42 | + .. code-block:: none |
| 43 | + |
| 44 | + ruby quickstart.rb |
| 45 | + |
| 46 | + The command line output contains details about the retrieved movie |
| 47 | + document: |
| 48 | + |
| 49 | + .. code-block:: none |
| 50 | + :copyable: false |
| 51 | + |
| 52 | + {"_id"=>BSON::ObjectId('...'), "plot"=>"A young man is accidentally sent |
| 53 | + 30 years into the past in a time-traveling DeLorean invented by his friend, |
| 54 | + Dr. Emmett Brown, and must make sure his high-school-age parents unite |
| 55 | + in order to save his own existence.", ... |
| 56 | + "title"=>"Back to the Future", ... |
| 57 | + |
| 58 | + If you encounter an error or see no output, ensure that you specified the |
| 59 | + correct connection string in the ``quickstart.rb`` file and that you loaded the |
| 60 | + sample data. |
| 61 | + |
| 62 | +After you complete these steps, you have a working application that |
| 63 | +uses the driver to connect to your MongoDB deployment, runs a query on |
| 64 | +the sample data, and prints out the result. |
| 65 | + |
| 66 | +.. include:: /includes/get-started/troubleshoot.rst |
0 commit comments