Class: EM::Mongo::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/em-mongo/connection.rb,
lib/em-mongo/prev.rb

Overview

An em-mongo Connection

Instance Method Summary collapse

Constructor Details

#initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {}) ⇒ Connection

Initialize and connect to a MongoDB instance

Parameters:

  • host (String) (defaults to: DEFAULT_IP)

    the host name or IP of the mongodb server to connect to

  • port (Integer) (defaults to: DEFAULT_PORT)

    the port the mongodb server is listening on

  • timeout (Integer) (defaults to: nil)

    the connection timeout



247
248
249
250
# File 'lib/em-mongo/connection.rb', line 247

def initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {})
  @em_connection = EMConnection.connect(host, port, timeout, opts)
  @db = {}
end

Instance Method Details

#closeObject

Close the connection to the database.



262
263
264
# File 'lib/em-mongo/connection.rb', line 262

def close
  @em_connection.close
end

#connected?true, false

Returns whether or not the connection is currently connected

Returns:

  • (true, false)

    whether or not the connection is currently connected



268
269
270
# File 'lib/em-mongo/connection.rb', line 268

def connected?
  @em_connection.connected?
end

#db(name = DEFAULT_DB) ⇒ EM::Mongo::Database

Return a database with the given name.

Parameters:

  • db_name (String)

    a valid database name.

Returns:



257
258
259
# File 'lib/em-mongo/connection.rb', line 257

def db(name = DEFAULT_DB)
  @db[name] ||= EM::Mongo::Database.new(name, self)
end

#db_and_col_name(full_name) ⇒ Object



46
47
48
49
# File 'lib/em-mongo/prev.rb', line 46

def db_and_col_name(full_name)
  parts = full_name.split(".")
  [ parts.shift, parts.join(".") ]
end

#delete(collection_name, selector) ⇒ Object



34
35
36
37
# File 'lib/em-mongo/prev.rb', line 34

def delete(collection_name, selector)
  db_name, col_name = db_and_col_name(collection_name)
  db(db_name).collection(col_name).remove(selector)
end

#find(collection_name, skip, limit, order, query, fields, &blk) ⇒ Object



39
40
41
42
43
44
# File 'lib/em-mongo/prev.rb', line 39

def find(collection_name, skip, limit, order, query, fields, &blk)
  db_name, col_name = db_and_col_name(collection_name)
  db(db_name).collection(col_name).find(query, :skip=>skip,:limit=>limit,:order=>order,:fields=>fields).defer_as_a.callback do |docs|
    yield docs if block_given?
  end
end

#insert(collection_name, documents) ⇒ Object



24
25
26
27
# File 'lib/em-mongo/prev.rb', line 24

def insert(collection_name, documents)
  db_name, col_name = db_and_col_name(collection_name)
  db(db_name).collection(col_name).insert(documents)
end

#send_command(*args, &block) ⇒ Object



272
# File 'lib/em-mongo/connection.rb', line 272

def send_command(*args, &block);@em_connection.send_command(*args, &block);end

#slave_ok?Boolean

Is it okay to connect to a slave?

Returns:

  • (Boolean)


277
# File 'lib/em-mongo/connection.rb', line 277

def slave_ok?;@em_connection.slave_ok?;end

#update(collection_name, selector, document, options = {}) ⇒ Object



29
30
31
32
# File 'lib/em-mongo/prev.rb', line 29

def update(collection_name, selector, document, options={})
  db_name, col_name = db_and_col_name(collection_name)
  db(db_name).collection(col_name).update(selector, document, options)
end