Configure Your MongoDB Connection
Configure application for MongoDB
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.
Specify target database in connection string
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
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.