I'm trying to extract data from mongodb to Elasticsearch, getMongodoc = coll.find().limit(10) will find the first 10 entries in mongo.
As you can see , result = ec.mongoConn should get result from method mongoConn() in class MongoConnector. when I use p hsh(to examine the output is correct), it will print 10 entires, while p result = ec.mongoConn will print #<Enumerator: #<Mongo::Cursor:0x70284070232580 @view=#<Mongo::Collection::View:0x70284066032180 namespace='mydatabase.mycollection' @filter={} @options={"limit"=>10}>>:each>
I changed p hsh to return hsh, p result = ec.mongoConn will get the correct result, but it just prints the first entry not all 10 entries. it seems that the value of hsh did not pass to result = ec.mongoConn correctly, Can anyone tell me what am I doing wrong? is this because I did something wrong with method calling?
class MongoConncetor def mongoConn() BSON::OrderedHash.new client = Mongo::Client.new([ 'xx.xx.xx.xx:27017' ], :database => 'mydatabase') coll = client[:mycollection] getMongodoc = coll.find().limit(10) getMongodoc.each do |document| hsh = symbolize_keys(document.to_hash).select { |hsh| hsh != :_id } return hsh # p hsh end end class ElasticConnector < MongoConncetor include Elasticsearch::API CONNECTION = ::Faraday::Connection.new url: 'http://localhost:9200' def perform_request(method, path, params, body) puts "--> #{method.upcase} #{path} #{params} #{body}" CONNECTION.run_request \ method.downcase.to_sym, path, (( body ? MultiJson.dump(body) : nil)), {'Content-Type' => 'application/json'} end ec = ElasticConnector.new p result = ec.mongoConn client = ElasticConnector.new client.bulk index: 'myindex', type:'test' , body: result end