Examples of em-mongo?

Looking at using em-mongo for a text parser script that loads text from db, parses it, marks keywords and updates db.

I would like to see some examples of em-mongo in action. Only one I could find was on the github em-mongo repo.

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 
+4
source share
1 answer

You do not need the next_tick method that em-mongo uses for you. Define callbacks that are executed if db actions are executed. Here is the skeleton:

 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 
+2
source

Source: https://habr.com/ru/post/1344918/


All Articles