IT Staff

Blog về chuyên ngành IT

Writing a Ruby program using MongoDB

leave a comment »

In the previous post, we’ve got the short introduction about the MongoDB and learn some experimenting with the mongodb javascript shell. Now, it’s time to get practical.

* Writing a Ruby program using MongoDB

My personally programming language is Ruby. So, I rather than like to jump into programing and taking my first look at the MongoDB driver for Ruby.

– First, install the MongoDB Ruby driver and connect to the database
Through using the RubyGems like this:

$ sudo gem install mongo

This will install both the mongo and bson gems.

Note: If the bson is not installed, just execute this command:

$ sudo gem install bson_ext

– Create a file called ‘connect.rb’ with the following code:

require ‘rubygems’
require ‘mongo’

@con = Mongo::Connection.new
@db = @con[‘honey_moon’]
@tours = @db[‘tours’]

So, now switch to the terminal window and run the this command:

$ ruby connect.rb

Bingo, no exceptions are throwed, we’re successfully loaded and connected to the MongoDB from the Ruby. Now we can cheer for that🙂

* Inserting data

You know:
– JSON is a document data structure.
– In Ruby, the hash data structure makes the most sense.

In this example, we use ‘irb’ (Ruby’s interactive REPL) to access the connection, database and collection objects.
Open the Terminal, enter the following command:

$ irb -r connect.rb

irb(main):001:0> id = @tours.save({“tour_name” => “Love You Love Me”})
=> BSON::ObjectId(‘502420010f53661081000001’)

irb(main):002:0> @tours.find_one({“_id” => id})
=> #BSON::ObjectId(‘502420010f53661081000001’), “tour_name”=>”Love You Love Me”}>

Next, I insert two new tours are Diamond Love and Gold Love. I create two variables with hash values:

irb(main):003:0> gold = {“tour_name” => “Gole Love”, “age” => 40}
=> {“age”=>40, “tour_name”=>”Gole Love”}

irb(main):004:0> diamond = {“tour_name” => “Diamond Love”, “age” => 60}
=> {“age”=>60, “tour_name”=>”Diamond Love”}

To insert these values to the document, we use insert method, and assign the returned values to two variables, see this:

irb(main):005:0> gold_id = @tours.insert(gold)
=> BSON::ObjectId(‘502423af0f53661081000002’)

irb(main):006:0> diamond_id = @tours.insert(diamond)
=> BSON::ObjectId(‘502423b90f53661081000003’)

Now, you can show it by using find_one method:

irb(main):007:0> @tours.find_one({“_id” => gold_id})
=> #BSON::ObjectId(‘502423af0f53661081000002’), “age”=>40, “tour_name”=>”Gole Love”}>

irb(main):008:0> @tours.find_one({“_id” => diamond_id})
=> #BSON::ObjectId(‘502423b90f53661081000003’), “age”=>60, “tour_name”=>”Diamond Love”}>

* Queries and cursors

By using find method to query the data with MongoDB:

irb(main):010:0> @tours.find({“tour_name” => “Diamond Love”})
=> “Diamond Love”} @cursor_id=>

irb(main):011:0> @tours.find({“age” => {“$gt” => 40}})
=> {“$gt”=>40}} @cursor_id=>

Tips: you can use the cursors buffer to fetch the results of the $gt query:

irb(main):017:0> cursor = @tours.find({“age” => {“$gt” => 30}})
=> {“$gt”=>30}} @cursor_id=>

irb(main):013:0> cursor.each do |doc|
irb(main):014:1* puts doc[“tour_name”]
irb(main):015:1* end
Gole Love
Diamond Love
=> nil

There’re two tours in the collections have age larger than 30.

* Updates and deletes

– You can use update method to modify the document easily.

irb(main):031:0> @tours.update({“tour_name” => “Diamond Love”}, {“$set” => {“age” => 80}})
=> 99

– Use remove method for removing the document/

irb(main):032:0> @tours.remove({“age” => {“$gte” => 40}})
=> true

Any comments to share your experiences is welcome!
Thanks for your attending and see you in my next post🙂

Written by Xavier

Tháng Tám 3, 2013 lúc 9:53 sáng

Posted in Staff

Tagged with

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s

%d bloggers like this: