MongoDB

Aidanmc
2 min readDec 20, 2020

We are looking for someone who knows how to use NoSQL databases. But what does that mean. Does it just mean that SQL isn't necessary? What it means is that the storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases.

How do non-tabular relations help us? In the end, it comes done to efficiency. With tabular relations, every single entry has to have the same data. You have to add a new column every time you want to add information for just one entry. When this one column is added it increases the amount of space needed equal to the number of entries. This is extremely inefficient and causes a lot of unused space. Instead of forcing every entry to have the same parameter, they are allowed to be fluid. Only adding the information that is necessary.

For MongoDB, this is thought of as a document structure. Every document is one instance of an object that stores the information for that object. Take 2 online shoppers. One is just buying a product once and the other is a frequent shopper at this store. The person who is buying once might not need the shop to store all of their information so they don't give it. While the other shops so frequently that they want an easy experience every time. These two people will have very different information save by the store. As the owner, you don't want to force the one-time shopper to give them all the information that isn't useful.

To get started with MongoDB is actually quite easy. First you need to add the gem to the ruby application that you are working on.

gem 'mongoid', '~> 7.0.5'

After you have installed it configuring the application comes next.

bin/rails g mongoid:config

At this point, you need to get webpacker installed onto your computer.

rails webpacker:install

Now you are ready to use MongoDB.

Working with it is actually quite similar to using any other database. Rails scaffolding still functions. There will be some differences with how the models look though:

class Comment
include Mongoid::Document

field :name, type: String
field :message, type: String

belongs_to :post
end

Now that you understand the basics go out and try. Its not as hard to use as you would think it would be.

--

--