<< All Posts

Ruby and MongoDB

Ruby and MongoDB

Hello internet,

I started working with Ruby and MongoDB 2 weeks ago. First of all, I would like to say that if you know another language like C, Java or even PHP if you jump into Ruby you’ll feel like doing math homework for your 12 years old kid. I’m not saying that Ruby is bad. I’m just saying that since you have a software development background you will pick up very very very fast.

Rails is a MVC framework that facilitates everything but since I’m coming from almost 2 years working with Symfony, ruby and rails is being piece of cake.

What I did? In my first week I completed the codecademy.com ruby course. I was talking with a co-worker and he said he doesn’t like codecademy because he feels that it’s too easy for him and Codecademy shows only basic stuff. Since I completed the course I can say my opinion now. I really liked. I know there are some basic ideas but I’m pretty sure that if you study Ruby and never touch your hand again in any code, I’ll probably forget a bunch of concepts.

I’m also read the book Beginning Rails 4. Now I’m reading other excellent book about Ruby and MongoDB. So, I decided to write this post in order to keep some commands I’ve learning in just one place in case I need in the future.

First some MongoDB useful commands:

Connecting and creating a db:

mongo name_of_my_database

Saving data into db:

db.name_of_my_object.save( { foo: 'bar', foo2: [ 'Bar2', 'Bar3']   })

Retrieving all data:


Retrieving data with equal filter:

db.name_of_my_object.find(foo: 'bar')

Retrieving data with partial search:

db.name_of_my_object.find({ foo: /bar/})

Retrieving data with partial search and insen sitive case

db.name_of_my_object.find({ foo: /bar/i})

Removing all the content:


Removing some specific the content:

db.name_of_my_object.remove({name: 'foo'})

Exporting data from db to json:

mongoexport –d name_of_your_db –c item

Exporting data to CSV:

mongoexport -d your_db_name -c your_table_name -f key1,key2 --csv -o output_file.csv

Exporting and creating a backup using mongodump:

mongodump -d database_name

Now some rails commands

We’re going to use the gem called Mongoid to talk with MongoDB from

<< All Posts


🇧🇷 🇨🇦
Runs on OpenBSD 🐡