Docs Menu
Docs Home
/ / /
Ruby Driver

Get Started with the Ruby Driver

The MongoDB Ruby Driver is a library that allows Ruby applications to interact with MongoDB databases. You can use the Ruby driver to connect to MongoDB and perform common data operations. This guide shows you how to create an application that uses the Ruby driver to connect to a MongoDB cluster hosted on MongoDB Atlas and query data in your cluster.

Tip

MongoDB Atlas is a fully managed cloud database service that hosts your MongoDB deployments. You can create your own free (no credit card required) MongoDB Atlas deployment by following the steps in this guide.

If you prefer to use a different driver or programming language to connect to MongoDB, see our list of official drivers.

Note

If you run into issues on this step, ask for help in the MongoDB Stack Overflow tag or the MongoDB Reddit community. You can also submit feedback by using the Rate this page tab on the right side of this page.

1

Before you begin developing, ensure you install Ruby version 2.7 or later in your development environment. Ruby is pre-installed on macOS and some Linux distributions, but you might need to update your version.

Important

The Ruby driver is not officially supported on Windows.

2

Run the following command in your shell to create a directory called ruby-quickstart for this project:

mkdir ruby-quickstart

Then, run the following commands to create a quickstart.rb file in the ruby-quickstart directory:

cd ruby-quickstart
touch quickstart.rb
3

Open the quickstart.rb file and add the following code:

require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mongo'
end

This code adds the Ruby driver as a dependency by using the Bundler dependency management tool.

After you complete these steps, you have a new project directory with the driver dependencies installed.

You can create a free tier MongoDB deployment on MongoDB Atlas to store and manage your data. MongoDB Atlas hosts and manages your MongoDB database in the cloud.

1

Complete the Get Started with Atlas guide to set up a new Atlas account and load sample data into a new free tier MongoDB deployment.

2

After you create your database user, save that user's username and password to a safe location for use in an upcoming step.

After you complete these steps, you have a new free tier MongoDB deployment on Atlas, database user credentials, and sample data loaded in your database.

You can connect to your MongoDB deployment by providing a connection URI, also called a connection string, which instructs the driver how to connect to a MongoDB deployment and how to behave while connected.

The connection string includes the hostname or IP address and port of your deployment, the authentication mechanism, user credentials when applicable, and connection options.

To learn how to connect to an instance or deployment not hosted on Atlas, see the Choose a Connection Target guide.

1

To retrieve your connection string for the deployment that you created in the previous step, log in to your Atlas account and navigate to the Clusters section. Then, click the Connect button for your new deployment.

The connect button in the clusters section of the Atlas UI

Proceed to the Connect your application section. Select "Ruby" from the Driver selection menu and the version that best matches your installed version from the Version selection menu.

2

Click the copy button on the right of the connection string to copy it to your clipboard, as shown in the following screenshot:

The connection string copy button in the Atlas UI
3

Paste this connection string into a file in your preferred text editor and replace the <db_username> and <db_password> placeholders with your database user's username and password.

Save this file to a safe location for use in the next step.

After completing these steps, you have a connection string that contains your database username and password.

1

Navigate to your quickstart.rb file in the ruby-quickstart directory. Copy and paste the following code below the Bundler code from the Download and Install step of this tutorial. This code connects to MongoDB and queries the movies collection in the sample_mflix database.

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
2

Replace the <connection string> placeholder with the connection string that you copied from the Create a Connection String step of this tutorial.

3

From your ruby-quickstart directory, run the following shell command to run the application:

ruby quickstart.rb

The command line output contains details about the retrieved movie document:

{"_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.

Congratulations on completing the quick start tutorial!

In this tutorial, you created a Ruby application that connects to a MongoDB deployment hosted on MongoDB Atlas and retrieves a document that matches a query.

Learn more about the Ruby driver from the following resources:

  • Learn how to perform read operations in the Query Documents section.

  • Learn how to perform write operations in the Insert Documents section.

Back

Overview