Skip to content

Commit 1dc855c

Browse files
authored
DOCSP-45175: Connect to MongoDB (#95)
* DOCSP-45175: Connect to MongoDB * fixes * edits, next steps * JS feedback * DR feedback
1 parent e681751 commit 1dc855c

File tree

5 files changed

+123
-10
lines changed

5 files changed

+123
-10
lines changed

source/get-started.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Get Started with the Ruby Driver
2323
Download & Install </get-started/download-and-install/>
2424
Create a Deployment </get-started/create-a-deployment>
2525
Create a Connection String </get-started/create-a-connection-string>
26-
27-
.. TODO:
2826
Connect to MongoDB </get-started/connect-to-mongodb>
2927
Next Steps </get-started/next-steps>
3028

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

source/get-started/download-and-install.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ Download and Install
4747

4848
Open the ``quickstart.rb`` file and add the following code:
4949

50-
.. code-block:: ruby
51-
52-
require 'bundler/inline'
53-
54-
gemfile do
55-
source 'https://rubygems.org'
56-
gem 'mongo'
57-
end
50+
.. literalinclude:: /includes/get-started/quickstart.rb
51+
:language: ruby
52+
:dedent:
53+
:start-after: start-bundler
54+
:end-before: end-bundler
5855

5956
This code adds the {+driver-short+} as a dependency by
6057
using the `Bundler <https://bundler.io/>`__ dependency management tool.

source/get-started/next-steps.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.. _ruby-get-started-next-steps:
2+
3+
==========
4+
Next Steps
5+
==========
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: learn more
13+
14+
Congratulations on completing the quick start tutorial!
15+
16+
In this tutorial, you created a {+language+} application that
17+
connects to a MongoDB deployment hosted on MongoDB Atlas
18+
and retrieves a document that matches a query.
19+
20+
.. TODO:
21+
Learn more about {+driver-short+} from the following resources:
22+
- Learn how to perform read operations in the :ref:`<ruby-read>` section.
23+
- Learn how to perform write operations in the :ref:`<ruby-write>` section.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# start-bundler
2+
require 'bundler/inline'
3+
4+
gemfile do
5+
source 'https://rubygems.org'
6+
gem 'mongo'
7+
end
8+
# end-bundler
9+
10+
# start-query
11+
uri = '<connection string>'
12+
13+
begin
14+
client = Mongo::Client.new(uri)
15+
16+
database = client.use('sample_mflix')
17+
movies = database[:movies]
18+
19+
# Queries for a movie that has the title 'Back to the Future'
20+
query = { title: 'Back to the Future' }
21+
movie = movies.find(query).first
22+
23+
# Prints the movie document
24+
puts movie
25+
26+
ensure
27+
client&.close
28+
end
29+
# end-query

0 commit comments

Comments
 (0)