class FileReader::MongoDB

Contains the reading functionalities of MongoDB data

Attributes

data[R]

Public Class Methods

new(col_name, coll_name, db_name: 'ccsdm', host: 'localhost', port: '27017') click to toggle source
Calls superclass method Connector::MongoConnector::new
# 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

Public Instance Methods

read(hide: { '_id' => 0 }, q_meth: :agg, match: {}) click to toggle source
# 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