Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

Join us at AWS re:Invent 2024! Learn how to use MongoDB for AI use cases.
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:
1require 'bundler/inline'
2
3gemfile do
4 source 'https://rubygems.org'
5 gem 'mongo'
6end
7
8client = Mongo::Client.new('mongodb+srv://username:password@mycluster.mongodb.net/test')
9collection = client[:foo]
10collection.insert_one({ bar: "baz" })
11
12puts collection.find.first
13# => {"_id"=>BSON::ObjectId('62d83d9dceb023b20aff228a'), "bar"=>"baz"}
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:
1# set a new field 'counter' to 1
2collection.update_one({ _id: BSON::ObjectId('62d83d9dceb023b20aff228a')}, :"$set" => { counter: 1 })
3
4puts collection.find.first
5# => {"_id"=>BSON::ObjectId('62d83d9dceb023b20aff228a'), "bar"=>"baz", "counter"=>1}
6
7# increment the field 'counter' by one
8collection.update_one({ _id: BSON::ObjectId('62d83d9dceb023b20aff228a')}, :"$inc" => { counter: 1 })
9
10puts collection.find.first
11# => {"_id"=>BSON::ObjectId('62d83d9dceb023b20aff228a'), "bar"=>"baz", "counter"=>2}
12
13# remove the test document
14collection.delete_one({ _id: BSON::ObjectId('62d83d9dceb023b20aff228a') })

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:
1require 'bundler/inline'
2
3gemfile do
4 source 'https://rubygems.org'
5
6 gem 'mongoid'
7end
8
9Mongoid.configure do |config|
10 config.clients.default = { uri: "mongodb+srv://username:password@mycluster.mongodb.net/test" }
11end
12
13class Foo
14 include Mongoid::Document
15
16 field :bar, type: String
17 field :counter, type: Integer, default: 1
18end
19
20# create a new instance of 'Foo', which will assign a default value of 1 to the 'counter' field
21foo = Foo.create bar: "baz"
22
23puts foo.inspect
24# => <Foo _id: 62d84be3ceb023b76a48df90, bar: "baz", counter: 1>
25
26# interact with the instance variable 'foo' and modify fields programmatically
27foo.counter += 1
28
29# save the instance of the model, persisting changes back to MongoDB
30foo.save!
31
32puts foo.inspect
33# => <Foo _id: 62d84be3ceb023b76a48df90, bar: "baz", counter: 2>

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
News & Announcements

MongoDB Academia - Introduction to Modern Databases


Sep 23, 2022 | 4 min read
Article

Securing MongoDB with TLS


May 09, 2022 | 4 min read
Tutorial

Introduction to LangChain and MongoDB Atlas Vector Search


Jan 12, 2024 | 5 min read
News & Announcements

Laravel MongoDB 5.0: Major Release With Exciting New Features!


Sep 13, 2024 | 1 min read
Table of Contents