Class: Moped::Session

Inherits:
Object show all
Defined in:
lib/moped/session.rb,
lib/moped/session/context.rb

Overview

A session in moped is root for all interactions with a MongoDB server or replica set.

It can talk to a single default database, or dynamically speak to multiple databases.

Examples:

Single database (console-style)

session = Moped::Session.new(["127.0.0.1:27017"])
session.use :moped
session[:users].find.one # => { name: "John" }

Multiple databases

session = Moped::Session.new(["127.0.0.1:27017"])

session.with(database: :admin) do |admin|
  admin.command ismaster: 1
end

session.with(database: :moped) do |moped|
  moped[:users].find.one # => { name: "John" }
end

Authentication


session = Moped::Session.new %w[127.0.0.1:27017],
session.with(database: "admin").("admin", "s3cr3t")

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seeds, options = {}) ⇒ Session

Initialize a new database session.

Examples:

Initialize a new session.

Session.new([ "localhost:27017" ])

Parameters:

  • seeds (Array)

    an of host:port pairs

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :safe (Boolean) — default: false

    Ensure writes are persisted.

  • :safe (Hash)

    Ensure writes are persisted with the specified safety level e.g., “fsync: true”, or “w: 2, wtimeout: 5”.

  • :database (Symbol, String)

    The database to use.

  • :consistency (:strong, :eventual) — default: :eventual

    .

  • :max_retries (Integer)

    The maximum number of attempts to retry an operation. (30)

  • :retry_interval (Integer)

    The time in seconds to retry connections to a secondary or primary after a failure. (1)

Since:

  • 1.0.0



186
187
188
189
190
191
# File 'lib/moped/session.rb', line 186

def initialize(seeds, options = {})
  @cluster = Cluster.new(seeds, {})
  @context = Context.new(self)
  @options = options
  @options[:consistency] ||= :eventual
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



35
36
37
# File 'lib/moped/session.rb', line 35

def cluster
  @cluster
end

#cluster The session cluster.(Thesessioncluster.) ⇒ Object (readonly)



35
# File 'lib/moped/session.rb', line 35

attr_reader :cluster, :context, :options

#contextObject (readonly)

Returns the value of attribute context.



35
36
37
# File 'lib/moped/session.rb', line 35

def context
  @context
end

#context The session context.(Thesessioncontext.) ⇒ Object (readonly)



35
# File 'lib/moped/session.rb', line 35

attr_reader :cluster, :context, :options

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/moped/session.rb', line 35

def options
  @options
end

#options The session options.(Thesessionoptions.) ⇒ Object (readonly)



35
# File 'lib/moped/session.rb', line 35

attr_reader :cluster, :context, :options

Instance Method Details

#[](name) ⇒ Collection

Return collection from the current database.

Parameters:

Returns:

Since:

  • 1.0.0



44
45
46
# File 'lib/moped/session.rb', line 44

def [](name)
  current_database[name]
end

#collection_namesArray<String>

Return non system collection name from the current database.

Returns:

Since:

  • 1.0.0



55
56
57
# File 'lib/moped/session.rb', line 55

def collection_names
  current_database.collection_names
end

#collectionsArray<Collection>

Return non system collection name from the current database.

Returns:

Since:

  • 1.0.0



66
67
68
# File 'lib/moped/session.rb', line 66

def collections
  current_database.collections
end

#command(op) ⇒ Hash

Run command on the current database.

Parameters:

  • command (Hash)

    The command to run.

Returns:

  • (Hash)

    the result of the command.

Since:

  • 1.0.0



77
78
79
# File 'lib/moped/session.rb', line 77

def command(op)
  current_database.command(op)
end

#consistency:strong, :eventual

Get the session’s consistency.

Examples:

Get the session consistency.

session.consistency

Returns:

  • (:strong, :eventual)

    The session’s consistency.

Since:

  • 1.0.0



163
164
165
# File 'lib/moped/session.rb', line 163

def consistency
  options[:consistency]
end

#database_namesArray<String>

Note:

This requires admin access on your server.

Get a list of all the database names for the session.

Examples:

Get all the database names.

session.database_names

Returns:

Since:

  • 1.2.0



91
92
93
# File 'lib/moped/session.rb', line 91

def database_names
  databases["databases"].map { |database| database["name"] }
end

#databasesHash

Note:

This requires admin access on your server.

Get information on all databases for the session. This includes the name, size on disk, and if it is empty or not.

Examples:

Get all the database information.

session.databases

Returns:

  • (Hash)

    The hash of database information, under the “databases” key.

Since:

  • 1.2.0



107
108
109
# File 'lib/moped/session.rb', line 107

def databases
  with(database: :admin).command(listDatabases: 1)
end

#disconnecttrue

Disconnects all nodes in the session’s cluster. This should only be used in cases # where you know you’re not going to use the cluster on the thread anymore and need to force the connections to close.

Returns:

  • (true)

    True if the disconnect succeeded.

Since:

  • 1.2.0



118
119
120
# File 'lib/moped/session.rb', line 118

def disconnect
  cluster.disconnect
end

#dropnil

Drop the current database.

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



129
130
131
# File 'lib/moped/session.rb', line 129

def drop
  current_database.drop
end

#login(username, password) ⇒ Object

Log in with username and password on the current database.

Parameters:

  • username (String)

    The username.

  • password (String)

    The password.

Since:

  • 1.0.0



140
141
142
# File 'lib/moped/session.rb', line 140

def (username, password)
  current_database.(username, password)
end

#logoutObject

Log out from the current database.

Since:

  • 1.0.0



151
152
153
# File 'lib/moped/session.rb', line 151

def logout
  current_database.logout
end

#new(options = {}) {|session| ... } ⇒ Session

Create a new session with options and use new socket connections.

Examples:

Change safe mode

session.with(safe: { w: 2 })[:people].insert(name: "Joe")

Change safe mode with block

session.with(safe: { w: 2 }) do |session|
  session[:people].insert(name: "Joe")
end

Temporarily change database

session.with(database: "admin") do |admin|
  admin.command ismaster: 1
end

Copy between databases

session.use "moped"
session.with(database: "backup") do |backup|
  session[:people].each do |person|
    backup[:people].insert person
  end
end

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Yield Parameters:

  • session (Session)

    The new session.

Returns:

See Also:

Since:

  • 1.0.0



225
226
227
228
229
230
231
232
233
# File 'lib/moped/session.rb', line 225

def new(options = {})
  session = with(options)
  session.instance_variable_set(:@cluster, cluster.dup)
  if block_given?
    yield session
  else
    session
  end
end

#safe?true, false

Is the session operating in safe mode?

Examples:

Is the session operating in safe mode?

session.safe?

Returns:

  • (true, false)

    Whether the current session requires safe operations.

Since:

  • 1.0.0



244
245
246
# File 'lib/moped/session.rb', line 244

def safe?
  !!safety
end

#safetyBoolean, Hash

Get the safety level for the session.

Examples:

Get the safety level.

session.safety

Returns:

  • (Boolean, Hash)

    The safety level for this session.

Since:

  • 1.0.0



256
257
258
# File 'lib/moped/session.rb', line 256

def safety
  options[:safe].__safe_options__
end

#use(database) ⇒ Object

Switch the session’s current database.

Examples:

Switch the current database.

session.use :moped
session[:people].find.one # => { :name => "John" }

Parameters:

Since:

  • 1.0.0



269
270
271
272
# File 'lib/moped/session.rb', line 269

def use(database)
  options[:database] = database
  set_current_database database
end

#with(options = {}) {|session| ... } ⇒ Session, Object

Create a new session with options reusing existing connections.

Examples:

Change safe mode

session.with(safe: { w: 2 })[:people].insert(name: "Joe")

Change safe mode with block

session.with(safe: { w: 2 }) do |session|
  session[:people].insert(name: "Joe")
end

Temporarily change database

session.with(database: "admin") do |admin|
  admin.command ismaster: 1
end

Copy between databases

session.use "moped"
session.with(database: "backup") do |backup|
  session[:people].each do |person|
    backup[:people].insert person
  end
end

Parameters:

  • options (Hash) (defaults to: {})

    The session options.

Yield Parameters:

  • session (Session)

    The new session.

Returns:

  • (Session, Object)

    The new session, or the value returned by the block if provided.

Since:

  • 1.0.0



305
306
307
308
309
310
311
312
313
# File 'lib/moped/session.rb', line 305

def with(options = {})
  session = dup
  session.options.update(options)
  if block_given?
    yield session
  else
    session
  end
end