Class: EM::Mongo::Connection
- Inherits:
-
Object
- Object
- EM::Mongo::Connection
- Defined in:
- lib/em-mongo/connection.rb,
lib/em-mongo/prev.rb
Overview
An em-mongo Connection
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection to the database.
-
#connected? ⇒ true, false
Whether or not the connection is currently connected.
-
#db(name = DEFAULT_DB) ⇒ EM::Mongo::Database
Return a database with the given name.
- #db_and_col_name(full_name) ⇒ Object
- #delete(collection_name, selector) ⇒ Object
- #find(collection_name, skip, limit, order, query, fields, &blk) ⇒ Object
-
#initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {}) ⇒ Connection
constructor
Initialize and connect to a MongoDB instance.
- #insert(collection_name, documents) ⇒ Object
- #send_command(*args, &block) ⇒ Object
-
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?.
- #update(collection_name, selector, document, options = {}) ⇒ Object
Constructor Details
#initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {}) ⇒ Connection
Initialize and connect to a MongoDB instance
246 247 248 249 |
# File 'lib/em-mongo/connection.rb', line 246 def initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {}) @em_connection = EMConnection.connect(host, port, timeout, opts) @db = {} end |
Instance Method Details
#close ⇒ Object
Close the connection to the database.
261 262 263 |
# File 'lib/em-mongo/connection.rb', line 261 def close @em_connection.close end |
#connected? ⇒ true, false
Returns whether or not the connection is currently connected.
267 268 269 |
# File 'lib/em-mongo/connection.rb', line 267 def connected? @em_connection.connected? end |
#db(name = DEFAULT_DB) ⇒ EM::Mongo::Database
Return a database with the given name.
256 257 258 |
# File 'lib/em-mongo/connection.rb', line 256 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
271 |
# File 'lib/em-mongo/connection.rb', line 271 def send_command(*args, &block);@em_connection.send_command(*args, &block);end |
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?
276 |
# File 'lib/em-mongo/connection.rb', line 276 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, ={}) db_name, col_name = db_and_col_name(collection_name) db(db_name).collection(col_name).update(selector, document, ) end |