Hendra Nicholas I am a software engineer, consultant and CEO at 41studio currently living in West Java, Indonesia. My interests range from programming to sport. I am also interested in entrepreneurship, technology, and design.

Introducing MongoDB

1 min read

Introducing MongoDB

MongoDB is the one of noSQL database, empowering businesses to be more agile and scalable. MongoDB is a general purpose, open-source database. It’s features are :

  • Document data model with dynamic schemas
  • Full, flexible index support and rich queries
  • Auto-Sharding for horizontal scalability
  • Built-in replication for high availability
  • Text search
  • Advendced security
  • Aggregation Framework and MapReduce
  • Large media storage with GridFS

Table of Comparison MongoDB vs MySQL

DatabaseOperationTotal RowsThreadsNumber of Selected RowsRun Time (Select + Fetch)CPU Usage (4 cores)Select Time
MongoInsert1000000080411121 ms137%CPU0
MySQLInsert10000000801130493 ms149%CPU0
MongoSelect+Fetch100000001500017411 ms105%CPU3 ms
MySQLSelect+Fetch10000000150005836 ms109%CPU5369 ms
MongoSelect+Fetch10000000150006450 ms116%CPU2 ms
MySQLSelect+Fetch1000000015000512 ms208%CPU66 ms
MongoSelect+Fetch100000002500012507 ms110%CPU3 ms
MySQLSelect+Fetch1000000025000565 ms236%CPU69 ms
MongoSelect+Fetch100000004500028129 ms106%CPU2 ms
MySQLSelect+Fetch1000000045000592 ms255%CPU72 ms
MongoSelect+Fetch100000008500075047 ms64%CPU1 ms
MySQLSelect+Fetch1000000085000759 ms275%CPU66 ms

Install MongoDB in Ubuntu

The following command to install the latest stable version of MongoDB :

[code language="bash"]
 sudo apt-get install mongodb-10gen
[/code]

Controlling MongoDB

1. Starting MongoDB

[code language="bash"]
 sudo service mongodb start
[/code]

2. Stoping MongoDB

[code language="bash"]
 sudo service mongodb stop
[/code]

3. Restarting MongoDB

[code language="bash"]
 sudo service mongodb restart
[/code]

To use MongoDB in rails application, there are simple ways. We must following these steps :

  1. In your Gemfile add gem “mongoid”, github: “mongoid/mongoid”
  2. Run bundle install
  3. Run rails g mongoid:config to added mongoid.yml in your application.
  4. setup your mongoid.yml
[code language="ruby"]
 development: sessions: default: database: table_name hosts: - localhost:27017
[/code]

Note :

When you use MongoDB in you not need migration for processing CRUD, you just change in your model and save. Your table will change as your change in model.

4. Database Relation in MongoDB

[code language="ruby"]
 has_many belongs_to has_one has_and_belongs_to_many
[/code]

Ex :

[code language="ruby"]
 belongs_to
[/code]

5. Lets Say We Have Table a belongs_to b and b has_many a

In mongodb it will result like this :

[code language="ruby"]
 a = a.id,
 b.id b = b.id
[/code]

Note

In your model we don’t need to add b.id because mongo will automaticaly add b.id when you use relation

[code language="ruby"]
 has_and_belongs_to_many
[/code]

6. Lets Say We Have c has_and_belongs_to_many d and d has_and_belongs_to_many c in Mongodb It Will Result Like This :

[code language="ruby"]
 c = c.id,
 d.ids d = d.id,
 c.ids
[/code]

7. And Result of Data Will Be Like This :

[code language="ruby"]
 c = [001,[002]]
 d = [002,[001]]
[/code]

Query mongodb in rails app.

Select all : YourModel.all.to_a
where query : YourModel.where(:your_field => '').to_a
beetwen query : YourModel.where(:your_field.gt => values).to_a
condition in beetwen gt  = greater than gte = greater than equal lt = less than lte = less than equal

For more example query you can read on mongoid.org

Hendra Nicholas I am a software engineer, consultant and CEO at 41studio currently living in West Java, Indonesia. My interests range from programming to sport. I am also interested in entrepreneurship, technology, and design.