July 1, 2025
As developers, we often default to relational databases like PostgreSQL or MySQL when building Rails applications. But what happens when your data is better represented as documents, or you need more flexibility with your schema?
That’s where MongoDB comes in — and with the help of Mongoid , integrating it with Rails is smooth and effective.
In this article, I’ll show you how to set up MongoDB with Rails using Mongoid and create a more real-world example model: a digital library .
Step 1: Add Mongoid to Your Rails App
In your Gemfile, add:
gem 'mongoid', '~> 7.6'
Then run:
bundle install
Step 2: Generate the Mongoid Configuration
rails g mongoid:config
This command creates config/mongoid.yml, where you can define your database settings (host, port, database name, etc.).
Optional: Remove ActiveRecord if not needed
If you plan to use only MongoDB:
In config/application.rb, remove:
require "active_record/railtie"
Then remove or comment out database.yml.
Step 3: Create a Real-World Document Model – Book
Let’s imagine we’re building a digital library. Here’s a Book model with fields that reflect actual publishing data:
class Book
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :author, type: String
field :genre, type: String
field :isbn, type: String
field :language, type: String
field :published_year, type: Integer
field :pages, type: Integer
field :publisher, type: String
field :description, type: String
field :available, type: Boolean, default: true
end
Notice how easy it is to add new fields — no need to create migrations or worry about schema changes.
Step 4: Create and Query Documents
Open Rails console:
rails c
Then create a book:
Book.create(
title: "Siddhartha",
author: "Hermann Hesse",
genre: "Philosophical Fiction",
isbn: "9780142437186",
language: "English",
published_year: 1922,
pages: 152,
publisher: "Penguin",
description: "A spiritual journey of self-discovery",
)
Query books:
Book.where(author: "Hermann Hesse")
Book.where(available: true)
Book.find_by(isbn: "9780142437186")
Optional: Build a Simple API
Want to expose your data via a RESTful API?
Here’s an example controller:
class BooksController < ApplicationController
def index
render json: Book.all
end
def show
render json: Book.find(params[:id])
end
def create
book = Book.new(book_params)
if book.save
render json: book, status: :created
else
render json: book.errors, status: :unprocessable_entity
end
end
private
def book_params
params.require(:book).permit(
:title, :author, :genre, :isbn, :language,
:published_year, :pages, :publisher, :description, :available
)
end
end
Why Use MongoDB in Rails?
MongoDB is a great choice when:
- Your data is document-oriented
- The structure can vary across records
- You want to avoid complex joins
- You need faster prototyping
Mongoid makes the integration seamless — giving you a flexible way to work with modern NoSQL databases while enjoying the elegance of Rails.
Final Thoughts
If your app requires flexibility, rapid iteration, and a schema-less design, consider using MongoDB with Mongoid. It’s a powerful combination for modern Rails development.
Let me know if you’re using MongoDB in your own Rails apps — I’d love to hear how you’re leveraging it!
Top comments (0)