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
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. |
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
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