diff --git a/source/get-started.txt b/source/get-started.txt index 93a8ca25..bb100e54 100644 --- a/source/get-started.txt +++ b/source/get-started.txt @@ -23,8 +23,6 @@ Get Started with the Ruby Driver Download & Install Create a Deployment Create a Connection String - -.. TODO: Connect to MongoDB Next Steps diff --git a/source/get-started/connect-to-mongodb.txt b/source/get-started/connect-to-mongodb.txt new file mode 100644 index 00000000..40702b27 --- /dev/null +++ b/source/get-started/connect-to-mongodb.txt @@ -0,0 +1,66 @@ +.. _ruby-get-started-connect-to-mongodb: + +================== +Connect to MongoDB +================== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: test connection, runnable, code example + +.. procedure:: + :style: connected + + .. step:: Edit your {+language+} application file + + Navigate to your ``quickstart.rb`` file in the ``ruby-quickstart`` + directory. Copy and paste the following code below the Bundler + code from the :ref:`ruby-quick-start-download-and-install` step + of this tutorial. This code connects to MongoDB and queries the + ``movies`` collection in the ``sample_mflix`` database. + + .. literalinclude:: /includes/get-started/quickstart.rb + :language: ruby + :dedent: + :start-after: start-query + :end-before: end-query + + .. step:: Assign the connection string + + Replace the ```` placeholder with the + connection string that you copied from the :ref:`ruby-get-started-connection-string` + step of this tutorial. + + .. step:: Run your {+language+} application + + From your ``ruby-quickstart`` directory, run the following shell + command to run the application: + + .. code-block:: none + + ruby quickstart.rb + + The command line output contains details about the retrieved movie + document: + + .. code-block:: none + :copyable: false + + {"_id"=>BSON::ObjectId('...'), "plot"=>"A young man is accidentally sent + 30 years into the past in a time-traveling DeLorean invented by his friend, + Dr. Emmett Brown, and must make sure his high-school-age parents unite + in order to save his own existence.", ... + "title"=>"Back to the Future", ... + + If you encounter an error or see no output, ensure that you specified the + correct connection string in the ``quickstart.rb`` file and that you loaded the + sample data. + +After you complete these steps, you have a working application that +uses the driver to connect to your MongoDB deployment, runs a query on +the sample data, and prints out the result. + +.. include:: /includes/get-started/troubleshoot.rst diff --git a/source/get-started/download-and-install.txt b/source/get-started/download-and-install.txt index d2e0ee9e..18090fb6 100644 --- a/source/get-started/download-and-install.txt +++ b/source/get-started/download-and-install.txt @@ -47,14 +47,11 @@ Download and Install Open the ``quickstart.rb`` file and add the following code: - .. code-block:: ruby - - require 'bundler/inline' - - gemfile do - source 'https://rubygems.org' - gem 'mongo' - end + .. literalinclude:: /includes/get-started/quickstart.rb + :language: ruby + :dedent: + :start-after: start-bundler + :end-before: end-bundler This code adds the {+driver-short+} as a dependency by using the `Bundler `__ dependency management tool. diff --git a/source/get-started/next-steps.txt b/source/get-started/next-steps.txt new file mode 100644 index 00000000..eef41929 --- /dev/null +++ b/source/get-started/next-steps.txt @@ -0,0 +1,23 @@ +.. _ruby-get-started-next-steps: + +========== +Next Steps +========== + +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: learn more + +Congratulations on completing the quick start tutorial! + +In this tutorial, you created a {+language+} application that +connects to a MongoDB deployment hosted on MongoDB Atlas +and retrieves a document that matches a query. + +.. TODO: + Learn more about {+driver-short+} from the following resources: + - Learn how to perform read operations in the :ref:`` section. + - Learn how to perform write operations in the :ref:`` section. \ No newline at end of file diff --git a/source/includes/get-started/quickstart.rb b/source/includes/get-started/quickstart.rb new file mode 100644 index 00000000..92711d48 --- /dev/null +++ b/source/includes/get-started/quickstart.rb @@ -0,0 +1,29 @@ +# start-bundler +require 'bundler/inline' + +gemfile do + source 'https://rubygems.org' + gem 'mongo' +end +# end-bundler + +# start-query +uri = '' + +begin + client = Mongo::Client.new(uri) + + database = client.use('sample_mflix') + movies = database[:movies] + + # Queries for a movie that has the title 'Back to the Future' + query = { title: 'Back to the Future' } + movie = movies.find(query).first + + # Prints the movie document + puts movie + +ensure + client&.close +end +# end-query