Overview
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.
Download and Install
Install dependencies
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.
Add the Ruby driver to your project
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.
Create a MongoDB Deployment
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.
Create a free MongoDB deployment on Atlas
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.
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.
Create a Connection String
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.
Find your MongoDB Atlas connection string
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.

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.
After completing these steps, you have a connection string that contains your database username and password.
Connect to MongoDB
Edit your Ruby 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 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
Assign the connection string
Replace the <connection string>
placeholder with the
connection string that you copied from the Create a Connection String
step of this tutorial.
Run your Ruby application
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.
Next Steps
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.