Class Moped::Session
In: lib/moped/session.rb
Parent: Object

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.

@example Single database (console-style)

  session = Moped::Session.new(["127.0.0.1:27017"])
  session.use(:moped)
  session[:users].find.one

@example Multiple databases

  session = Moped::Session.new(["127.0.0.1:27017"])
  session.with(database: :admin) do |admin|
    admin.command(ismaster: 1)
  end

@example Authentication

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

@since 1.0.0

Methods

Included Modules

Optionable

Attributes

cluster  [R]  @!attribute cluster
  @return [ Cluster ] The cluster of nodes.

@!attribute options

  @return [ Hash ] The configuration options.
options  [R]  @!attribute cluster
  @return [ Cluster ] The cluster of nodes.

@!attribute options

  @return [ Hash ] The configuration options.

Public Class methods

Create a new session from a URI.

@example Initialize a new session.

  Session.connect("mongodb://localhost:27017/my_db")

@param [ String ] MongoDB URI formatted string.

@return [ Session ] The new session.

@since 3.0.0

Initialize a new database session.

@example Initialize a new session.

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

@param [ Array ] seeds An array of host:port pairs. @param [ Hash ] options The options for the session.

@see Above options validations for allowed values in the options hash.

@since 1.0.0

Public Instance methods

Return collection from the current database.

@param (see Moped::Database#[])

@return (see Moped::Database#[])

@since 1.0.0

Return non system collection name from the current database.

@param (see Moped::Database#collection_names)

@return (see Moped::Database#collection_names)

@since 1.0.0

Return non system collection name from the current database.

@param (see Moped::Database#collections)

@return (see Moped::Database#collections)

@since 1.0.0

Run command on the current database.

@param (see Moped::Database#command)

@return (see Moped::Database#command)

@since 1.0.0

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

@example Get all the database names.

  session.database_names

@note This requires admin access on your server.

@return [ Array<String>] All the database names.

@since 1.2.0

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

@example Get all the database information.

  session.databases

@note This requires admin access on your server.

@return [ Hash ] The hash of database information, under the "databases"

  key.

@since 1.2.0

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.

@return [ true ] True if the disconnect succeeded.

@since 1.2.0

Drop the current database.

@param (see Moped::Database#drop)

@return (see Moped::Database#drop)

@since 1.0.0

Provide a string inspection for the session.

@example Inspect the session.

  session.inspect

@return [ String ] The string inspection.

@since 1.4.0

Log in with username and password on the current database.

@param (see Moped::Database#login)

@raise (see Moped::Database#login)

@since 1.0.0

Log out from the current database.

@param (see Moped::Database#logout)

@raise (see Moped::Database#login)

@since 1.0.0

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

@example Change safe mode

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

@example Change safe mode with block

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

@example Temporarily change database

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

@example Copy between databases

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

@param [ Hash ] options The options.

@return [ Session ] The new session.

@see with

@since 1.0.0

@yieldparam [ Session ] session The new session.

Get the read preference for the session. Will default to primary if none was provided.

@example Get the session‘s read preference.

  session.read_preference

@return [ Object ] The read preference.

@since 2.0.0

Switch the session‘s current database.

@example Switch the current database.

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

@param [ String, Symbol ] database The database to use.

@since 1.0.0

Create a new session with options reusing existing connections.

@example Change safe mode

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

@example Change safe mode with block

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

@example Temporarily change database

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

@example Copy between databases

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

@param [ Hash ] options The session options.

@return [ Session, Object ] The new session, or the value returned

  by the block if provided.

@since 1.0.0

@yieldparam [ Session ] session The new session.

Get the write concern for the session. Will default to propagate if none was provided.

@example Get the session‘s write concern.

  session.write_concern

@return [ Object ] The write concern.

@since 2.0.0

[Validate]