class MongoDB::Connector

Public Class Methods

new(host,port) click to toggle source
# File lib/testSG/connector/mongo_db.rb, line 5
def initialize(host,port)
  @mongo_client = Mongo::MongoClient.new(host, port)
end

Public Instance Methods

insert(dbname, collection, doc) click to toggle source
# 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) click to toggle source
# 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