Class: MongoDB::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/testSG/connector/mongo_db.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Connector

Returns a new instance of Connector.



5
6
7
# File 'lib/testSG/connector/mongo_db.rb', line 5

def initialize(host,port)
  @mongo_client = Mongo::MongoClient.new(host, port)
end

Instance Method Details

#insert(dbname, collection, doc) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/testSG/connector/mongo_db.rb', line 9

def insert(dbname, collection, doc)
  # Connect to a specific DB within mongo
  db = @mongo_client.db(dbname)

  # Set the schema/collection in the db chosen
  coll = db.collection(collection)

  # Create a document that needs to be inserted in the collection
  # doc = {"name" => "MongoDB", "type" => "database", "count" => 1, "info" => {"x" => 203, "y" => '102'}}

  # Perform the insert
  id = coll.insert(doc)
end

#select(dbname, collection) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/testSG/connector/mongo_db.rb', line 23

def select(dbname, collection)
  # Connect to a specific DB within mongo
  db = @mongo_client.db(dbname)

  # Set the schema/collection in the db chosen
  coll = db.collection(collection)

  # Query the collection to view all the documents.
  coll.find.each { |row| puts row.inspect }
end