Skip to content

DOCSP-45175: Connect to MongoDB #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions source/get-started.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ Get Started with the Ruby Driver
Download & Install </get-started/download-and-install/>
Create a Deployment </get-started/create-a-deployment>
Create a Connection String </get-started/create-a-connection-string>

.. TODO:
Connect to MongoDB </get-started/connect-to-mongodb>
Next Steps </get-started/next-steps>

Expand Down
66 changes: 66 additions & 0 deletions source/get-started/connect-to-mongodb.txt
Original file line number Diff line number Diff line change
@@ -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 ``<connection string>`` 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
13 changes: 5 additions & 8 deletions source/get-started/download-and-install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://bundler.io/>`__ dependency management tool.
Expand Down
23 changes: 23 additions & 0 deletions source/get-started/next-steps.txt
Original file line number Diff line number Diff line change
@@ -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:`<ruby-read>` section.
- Learn how to perform write operations in the :ref:`<ruby-write>` section.
29 changes: 29 additions & 0 deletions source/includes/get-started/quickstart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# start-bundler
require 'bundler/inline'

gemfile do
source 'https://rubygems.org'
gem 'mongo'
end
# end-bundler

# start-query
uri = '<connection string>'

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
Loading