EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

Why Use MongoDB with Ruby

Alex Bevilacqua4 min read • Published Sep 30, 2022 • Updated Oct 07, 2022
MongoDBRuby
Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Before discovering Ruby and Ruby on Rails, I was a .NET developer. At that time, I'd make ad-hoc changes to my development database, export my table/function/stored procedure/view definitions to text files, and check them into source control with any code changes. Using diff functionality, I'd compare the schema changes that the DBAs needed to apply to production and we'd script that out separately.
I'm sure better tools existed (and I eventually started using some of RedGate's tools), but I was looking for a change. At that time, the real magic of Ruby on Rails for me was the Active Record Migrations which made working with my database fit with my programming workflow. Schema management became less of a chore and there were rake tasks for anything I needed (applying migrations, rolling back changes, seeding a test database).
Schema versioning and management with Rails was leaps and bounds better than what I was used to, and I didn't think this could get any better — but then I found MongoDB.
When working with MongoDB, there's no need to CREATE TABLE foo (id integer, bar varchar(255), ...); if a collection (or associated database) doesn't exist, inserting a new document will automatically create it for you. This means Active Record migrations are no longer needed as this level of schema change management was no longer necessary.
Having the flexibility to define my data model directly within the code without needing to resort to the intermediary management that Active Record had facilitated just sort of made sense to me. I could now persist object state to my database directly, embed related model details, and easily form queries around these structures to quickly retrieve my data.

Flexible schema

Data in MongoDB has a flexible schema as collections do not enforce a strict document structure or schema by default. This flexibility gives you data-modeling choices to match your application and its performance requirements, which aligns perfectly with Ruby's focus on simplicity and productivity.

Let's try it out

We can easily demonstrate how to quickly get started using the MongoDB Ruby Driver using the following simple Ruby script that will connect to a cluster, insert a document, and read it back:
When the document above is inserted, an _id value of BSON::ObjectId('62d83d9dceb023b20aff228a') is created. All documents must have an _id field. However, if not provided, a default _id of type ObjectId will be generated. When running the above, you will get a different value for _id, or you may choose to explicitly set it to any value you like!
Feel free to give the above example a spin using your existing MongoDB cluster or MongoDB Atlas cluster. If you don't have a MongoDB Atlas cluster, sign up for an always free tier cluster to get started.

Installation

The MongoDB Ruby Driver is hosted at RubyGems, or if you'd like to explore the source code, it can be found on GitHub.
To simplify the example above, we used bundler/inline to provide a single-file solution using Bundler. However, the mongo gem can be just as easily added to an existing Gemfile or installed via gem install mongo.

Basic CRUD operations

Our sample above demonstrated how to quickly create and read a document. Updating and deleting documents are just as painless as shown below:

Object document mapper

Though all interaction with your Atlas cluster can be done directly using the MongoDB Ruby Driver, most developers prefer a layer of abstraction such as an ORM or ODM. Ruby developers can use the Mongoid ODM to easily model MongoDB collections in their code and simplify interaction using a fluid API akin to Active Record's Query Interface.
The following example adapts the previous example to use Mongoid:

Summary

Whether you're using Ruby/Rails to build a script/automation tool, a new web application, or even the next Coinbase, MongoDB has you covered with both a Driver that simplifies interaction with your data or an ODM that seamlessly integrates your data model with your application code.

Conclusion

Interacting with your MongoDB data via Ruby — either using the Driver or the ODM — is straightforward, but you can also directly interface with your data from MongoDB Atlas using the built in Data Explorer. Depending on your preferences though, there are options:
  • MongoDB for Visual Studio Code allows you to connect to your MongoDB instance and enables you to interact in a way that fits into your native workflow and development tools. You can navigate and browse your MongoDB databases and collections, and prototype queries and aggregations for use in your applications.
  • MongoDB Compass is an interactive tool for querying, optimizing, and analyzing your MongoDB data. Get key insights, drag and drop to build pipelines, and more.
  • Studio 3T is an extremely easy to use 3rd party GUI for interacting with your MongoDB data.
  • MongoDB Atlas Data API lets you read and write data in Atlas with standard HTTPS requests. To use the Data API, all you need is an HTTPS client and a valid API key.
Ruby was recently added as a language export option to both MongoDB Compass and the MongoDB VS Code Extension. Using this integration you can easily convert an aggregation pipeline from either tool into code you can copy/paste into your Ruby application.

Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

MongoDB.Live 2020 Keynote In Less Than 10 Minutes


Mar 21, 2023 | 1 min read
Tutorial

Build a Modern Blog with Gatsby and MongoDB


Sep 23, 2022 | 16 min read
Tutorial

Preparing Time Series data for Analysis Tools with $densify and $fill


May 19, 2022 | 8 min read
Article

Building a Flask and MongoDB App with Azure Container Apps


Jun 20, 2023 | 8 min read
Table of Contents