Class: Mongo::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/connection.rb

Overview

A connection to MongoDB.

Direct Known Subclasses

Mongo

Constant Summary collapse

DEFAULT_PORT =
27017

Instance Method Summary collapse

Constructor Details

#initialize(pair_or_host = nil, port = nil, options = {}) ⇒ Connection

Create a Mongo database server instance. You specify either one or a pair of servers. If one, you also say if connecting to a slave is OK. In either case, the host default is “localhost” and port default is DEFAULT_PORT.

If you specify a pair, pair_or_host is a hash with two keys :left and :right. Each key maps to either

  • a server name, in which case port is DEFAULT_PORT

  • a port number, in which case server is “localhost”

  • an array containing a server name and a port number in that order

options are passed on to each DB instance:

:slave_ok

Only used if one host is specified. If false, when connecting to that host/port a DB object will check to see if the server is the master. If it is not, an error is thrown.

:auto_reconnect

If a DB connection gets closed (for example, we have a server pair and saw the “not master” error, which closes the connection), then automatically try to reconnect to the master or to the single server we have been given. Defaults to false.

Since that’s so confusing, here are a few examples:

Connection.new                         # localhost, DEFAULT_PORT, !slave
Connection.new("localhost")            # localhost, DEFAULT_PORT, !slave
Connection.new("localhost", 3000)      # localhost, 3000, slave not ok
# localhost, 3000, slave ok
Connection.new("localhost", 3000, :slave_ok => true)
# localhost, DEFAULT_PORT, auto reconnect
Connection.new(nil, nil, :auto_reconnect => true)

# A pair of servers. DB will always talk to the master. On socket
# error or "not master" error, we will auto-reconnect to the
# current master.
Connection.new({:left  => ["db1.example.com", 3000],
           :right => "db2.example.com"}, # DEFAULT_PORT
          nil, :auto_reconnect => true)

# Here, :right is localhost/DEFAULT_PORT. No auto-reconnect.
Connection.new({:left => ["db1.example.com", 3000]})

When a DB object first connects to a pair, it will find the master instance and connect to that one.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mongo/connection.rb', line 73

def initialize(pair_or_host=nil, port=nil, options={})
  @pair = case pair_or_host
           when String
             [[pair_or_host, port ? port.to_i : DEFAULT_PORT]]
           when Hash
            connections = []
            connections << pair_val_to_connection(pair_or_host[:left])
            connections << pair_val_to_connection(pair_or_host[:right])
            connections
           when nil
             [['localhost', DEFAULT_PORT]]
           end
  @options = options
end

Instance Method Details

#database_infoObject

Returns a hash containing database names as keys and disk space for each as values.



97
98
99
100
101
102
103
104
# File 'lib/mongo/connection.rb', line 97

def database_info
  doc = single_db_command('admin', :listDatabases => 1)
  h = {}
  doc['databases'].each { |db|
    h[db['name']] = db['sizeOnDisk'].to_i
  }
  h
end

#database_namesObject

Returns an array of database names.



107
108
109
# File 'lib/mongo/connection.rb', line 107

def database_names
  database_info.keys
end

#db(db_name, options = {}) ⇒ Object

Return the Mongo::DB named db_name. The slave_ok and auto_reconnect options passed in via #new may be overridden here. See DB#new for other options you can pass in.



91
92
93
# File 'lib/mongo/connection.rb', line 91

def db(db_name, options={})
  DB.new(db_name, @pair, @options.merge(options))
end

#drop_database(name) ⇒ Object

Drops the database name.



112
113
114
# File 'lib/mongo/connection.rb', line 112

def drop_database(name)
  single_db_command(name, :dropDatabase => 1)
end