Class: FileReader::MongoDB

Inherits:
Connector::MongoConnector show all
Defined in:
lib/mylookup/reader.rb

Overview

Contains the reading functionalities of MongoDB data

Instance Attribute Summary collapse

Attributes inherited from Connector::MongoConnector

#client, #coll, #coll_name, #db_name, #host, #port

Instance Method Summary collapse

Methods inherited from Connector::MongoConnector

#collection_exists?, #field_exists?, #recs

Constructor Details

#initialize(col_name, coll_name, db_name: 'ccsdm', host: 'localhost', port: '27017') ⇒ MongoDB

Returns a new instance of MongoDB.



33
34
35
36
37
# File 'lib/mylookup/reader.rb', line 33

def initialize(col_name, coll_name, db_name: 'ccsdm', host: 'localhost', port: '27017')
    super(coll_name, db_name: db_name, host: host, port: port) 
    @col = col_name
    @data = [] 
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



31
32
33
# File 'lib/mylookup/reader.rb', line 31

def data
  @data
end

Instance Method Details

#read(hide: { '_id' => 0 }, q_meth: :agg, match: {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mylookup/reader.rb', line 39

def read(hide: { '_id' => 0 }, q_meth: :agg, match: {})
    if q_meth == :find
        @coll.find(match).projection(hide).each do |doc|
            @data = @data + [doc[@col].to_s.downcase]
        end
        @data = @data.uniq
    elsif q_meth == :agg
        qry = [
            { '$match'   => match },
            { '$group'   => { '_id' => { @col => '$' + @col } } },
            { '$project' => { '_id' => 0, @col => '$_id.' + @col } }
        ]
        @coll.aggregate(qry).each do |doc|
            @data = @data + [doc[@col].to_s.downcase]
        end
    end
    return "Mongo Data contains #{@data.size} row(s)\nMongo First Record => #{@data[0]}"
end