Introducing MongoDB

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

 

Database

Operation

Total Rows

Threads

Number of Selected Rows

Run Time (Select + Fetch)

CPU Usage (4 cores)

Select Time

Mongo

Insert

10000000

8

0

411121 ms

137%CPU

0

MySQL

Insert

10000000

8

0

1130493 ms

149%CPU

0

Mongo

Select+Fetch

10000000

1

5000

17411 ms

105%CPU

3 ms

MySQL

Select+Fetch

10000000

1

5000

5836 ms

109%CPU

5369 ms

Mongo

Select+Fetch

10000000

1

5000

6450 ms

116%CPU

2 ms

MySQL

Select+Fetch

10000000

1

5000

512 ms

208%CPU

66 ms

Mongo

Select+Fetch

10000000

2

5000

12507 ms

110%CPU

3 ms

MySQL

Select+Fetch

10000000

2

5000

565 ms

236%CPU

69 ms

Mongo

Select+Fetch

10000000

4

5000

28129 ms

106%CPU

2 ms

MySQL

Select+Fetch

10000000

4

5000

592 ms

255%CPU

72 ms

Mongo

Select+Fetch

10000000

8

5000

75047 ms

64%CPU

1 ms

MySQL

Select+Fetch

10000000

8

5000

759 ms

275%CPU

66 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

 

%d bloggers like this: