Docs Menu
Docs Home
/ / /
Mongoid
/

Configure Your MongoDB Connection

1

To configure your application to use MongoDB and Mongoid as your ODM, run the following command from the root of your project:

bin/rails g mongoid:config

After the command completes successfully, your application contains the config/mongoid.yml file to configure the connection to the MongoDB deployment. Your application also includes the config/initializers/mongoid.rb file for more advanced configuration.

2

When connecting to an Atlas cluster, you must specify the database that you want to interact with as the default database in your connection string. You must add the database name to your connection string after the hostname.

The following example specifies the sample_restaurants target database in a sample connection string:

mongodb+srv://user0:pass123@mongo0.example.com/sample_restaurants
3

Paste the following configuration into the config/mongoid.yml file, making sure to replace the <connection string> placeholder with your connection string that references the target database:

development:
clients:
default:
uri: <connection string>

After completing these steps, your Rails web application is ready to connect to MongoDB.

Note

If you run into issues, ask for help in the MongoDB Community Forums or submit feedback by using the Feedback button in the upper right corner of the page.

Back

Create a Connection String