Class: Connector::MongoConnector
- Inherits:
-
Object
- Object
- Connector::MongoConnector
- Defined in:
- lib/mylookup/connector.rb
Overview
Defines MongoDB connection properties
Direct Known Subclasses
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#coll ⇒ Object
Returns the value of attribute coll.
-
#coll_name ⇒ Object
Returns the value of attribute coll_name.
-
#db_name ⇒ Object
Returns the value of attribute db_name.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #collection_exists? ⇒ Boolean
- #field_exists?(field) ⇒ Boolean
-
#initialize(coll_name, db_name: 'ccsdm', host: 'localhost', port: '27017') ⇒ MongoConnector
constructor
A new instance of MongoConnector.
-
#recs(qry) ⇒ Object
Queries and returns number of documents in the collection.
Constructor Details
#initialize(coll_name, db_name: 'ccsdm', host: 'localhost', port: '27017') ⇒ MongoConnector
Returns a new instance of MongoConnector.
11 12 13 14 15 16 17 |
# File 'lib/mylookup/connector.rb', line 11 def initialize(coll_name, db_name: 'ccsdm', host: 'localhost', port: '27017') @coll_name, @db_name, @host, @port = coll_name, db_name, host, port Mongo::Logger.logger.level = Logger::WARN @conn_str = @host + ':' + @port @client = Mongo::Client.new([@conn_str], :database => @db_name) @coll = @client[@coll_name] end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/mylookup/connector.rb', line 9 def client @client end |
#coll ⇒ Object
Returns the value of attribute coll.
9 10 11 |
# File 'lib/mylookup/connector.rb', line 9 def coll @coll end |
#coll_name ⇒ Object
Returns the value of attribute coll_name.
9 10 11 |
# File 'lib/mylookup/connector.rb', line 9 def coll_name @coll_name end |
#db_name ⇒ Object
Returns the value of attribute db_name.
9 10 11 |
# File 'lib/mylookup/connector.rb', line 9 def db_name @db_name end |
#host ⇒ Object
Returns the value of attribute host.
9 10 11 |
# File 'lib/mylookup/connector.rb', line 9 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
9 10 11 |
# File 'lib/mylookup/connector.rb', line 9 def port @port end |
Instance Method Details
#collection_exists? ⇒ Boolean
24 25 26 |
# File 'lib/mylookup/connector.rb', line 24 def collection_exists? recs({}) > 0 end |
#field_exists?(field) ⇒ Boolean
28 29 30 |
# File 'lib/mylookup/connector.rb', line 28 def field_exists? field @coll.find({}).projection({ '_id' => 0 }).limit(1).collect { |doc| doc }[0].keys.include? field end |
#recs(qry) ⇒ Object
Queries and returns number of documents in the collection
20 21 22 |
# File 'lib/mylookup/connector.rb', line 20 def recs qry @coll.find(qry).count end |