前邊有介紹mongodb的安裝以及ror項目的搭建,現在進行一下整合。
1.建立項目
建立項目時不再使用rails active_record支援
2.我們將要使用MongoMapper來驅動MongoDB到Rails
編輯GemFile,增加下面的內容
然後 執行 bundle install 安裝gem
3.添加資料庫連結
在config/initializer下面建立一個mongo.rb檔案,指定全域的資料庫資訊:
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)MongoMapper.database ='todo'#通過指定Rails運行環境參數,我們可以在不同的運行環境下建立互不干擾的資料,為了簡單起見,沒有為不同的環境指定不同的資料
if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process)do|forked| MongoMapper.connection.connectifforked endend
完成以上步驟後,啟動程式:
**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance.You can install the extension as follows:
If you continue to receive this message after installing, make sure that thebson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.=> Booting WEBrick=> Rails 3.0.10 application starting in development on http://0.0.0.0:3000=> Call with -d to detach=> Ctrl-C to shutdown server[2011-10-19 23:36:14] INFO WEBrick 1.3.1[2011-10-19 23:36:14] INFO ruby 1.9.2 (2011-07-09) [x86_64-linux][2011-10-19 23:36:14] INFO WEBrick::HTTPServer#start: pid=19595 port=3000
從上面輸出中可以看到bson_ext庫沒有載入。按照提示安裝該庫即可(別忘了在gemfile中添加gem):
再次啟動程式,Notice提示訊息消失,啟動正常。在瀏覽器輸入:http://127.0.0.1:3000,就可以看到如下頁面
4.添加頁面和處理邏輯
通過rails的generate命令來產生頁面、控制器和模型層檔案(個人還是喜歡自己手動建立,這裡為了示範方便)
rails generate scaffold project name:string --orm=mongo_mapper
由於我們使用mongo作為資料庫。那麼,我們需要把ActiveRecord的model,改成MongoMapper的類型,也就是把繼承關係從ActiveRecord::Base變成MongoMapper::Document。我們使用key這個方法標明該MongoMapper的欄位屬性。我們的屬性是name,再加上這個欄位的類型String,那麼定義如下:
classProject include MongoMapper::Document key:name,Stringend
通過以上的修改,我們就已經擁有了所有添加,更新,刪除和列表的操作
5.資料查看
可以通過命令mongo進入mongodb資料庫進行資料的查詢
mongo //進入資料庫use todo //切換庫db.projects.find() //執行查詢
6.其他
MongoMapper和ActiveRecord是完全相同的。甚至,MongoMapper還是支援ActiveRecord的驗證方式如下
validates_presence_of:name
由於MongoDB沒有schema-less(資料版本記錄)我們可以非常容易的添加和更改model的屬性,而不需要執行任何migrations的操作。比如,我們需要添加一個priority的屬性,我們僅僅需要的是修改Project model如下:
classProject include MongoMapper::Document key:name,String,:required=>true key:priority,Integerend
表之間的關聯對於MongoDB這裡稍微有點區別,我們需要ObjectId類型來儲存所有id。
至於,處理不同表之前的關聯,我們可以像ActiveRecord一樣定義belongs_to,當然,稍微有點不同,在Project中我們需要定義has_many :tasks,在MongoMapper中需要用many代替。
我目前也就做到這裡。有時間再去深入研究其他的功能。
PS:Ruby編寫MongoDB備份指令碼(fsync & lock)
#!/usr/local/bin/ruby# date: 06-12-2014# auther: lucifer# use fsync and lock to the file-system before backup the file-system# mongo-ruby-driver version > 1.10.0require 'mongo'require 'fileutils'require 'date'include Mongoinclude BSON# the members of replcation-set# test mongodb server version 2.6.0# host = "192.168.11.51"# The port of members# If the port is 27017 by default then otherport don't need to assignment# otherport = ""# port = otherport.length != 0 ? otherport : MongoClient::DEFAULT_PORT# opts = {:pool_size => 5, :pool_timeout => 10}# Create a new connection# client = MongoClient.new(host, port, opts)uri_string = "mongodb://caoqing:xxxxxxxx@x.x.x.x:27017/admin"client = MongoClient.from_uri(uri = "#{uri_string}")db = client['admin']# fsync and lock the databasecmd = OrderedHash.newcmd[:fsync] = 1cmd[:lock] = true# p cmddb.command(cmd)# datafile pathd = "/var/lib/mongo"# dir = Dir.new("#{d}")# entries = dir.entries# entries.delete_if { |entry| entry =~ /^\./}# convert the relative path to the full path# entries.map! { |entry| File.join(dir.path, entry) }# maintain only the type of file# entries.delete_if { |entry| !File.file?(entry) }# p entriesstart = Date.today.to_sprev = (Date.today - 7).to_sdest = "/backup/#{start}"sour = "/backup/#{prev}"FileUtils.rm_rf("#{sour}") if File::exist?("#{sour}")Dir.mkdir("#{dest}", 0755) unless File::exist?("#{dest}")FileUtils.cp_r Dir.glob("#{d}/**"), dest if client.locked?puts "*" * 20puts "\tbackup complete"puts "*" * 20# DB::SYSTEM_COMMAND_COLLECTION# unlock the databasedb["$cmd.sys.unlock"].find_oneclient.close