2011-03-23 27 views
4

尋找使用em-mongo的文本分析器腳本,它從db中加載文本,分析它,標記關鍵字並更新db。em-mongo的例子?

很想看到em-mongo在行動中的一些例子。只有一個我能找到的是github em-mongo回購。

require 'em-mongo' 

    EM.run do 
    db = EM::Mongo::Connection.new.db('db') 
    collection = db.collection('test') 
    EM.next_tick do 
     doc = {"hello" => "world"} 
     id = collection.insert(doc) 
     collection.find('_id' => id]) do |res| 
     puts res.inspect 
     EM.stop 
     end 
     collection.remove(doc) 
    end 
    end 

回答

2

你不需要next_tick方法,那就是em-mongo爲你做的。定義回調,如果數據庫操作完成,則執行回調。這是一個骨架:

class NonBlockingFetcher 
    include MongoConfig 

    def initialize 
    configure 
    @connection = EM::Mongo::Connection.new(@server, @port) 
    @collection = init_collection(@connection) 
    end 

    def fetch(value) 
    mongo_cursor = @collection.find({KEY => value.to_s}) 
    response = mongo_cursor.defer_as_a 

    response.callback do |documents| 
     # foo 
     # get one document 
     doc = documents.first 
    end 

    response.errback do |err| 
     # foo 
    end 

    end 
end