Class: Moped::Database
Overview
The class for interacting with a MongoDB database. One only interacts with this class indirectly through a session.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
- #name The name of the database.(Thenameofthedatabase.) ⇒ Object readonly
-
#session ⇒ Object
readonly
Returns the value of attribute session.
- #session The session.(Thesession.) ⇒ Object readonly
Instance Method Summary collapse
-
#[](collection) ⇒ Collection
Get a collection by the provided name.
-
#collection_names ⇒ Array<String>
Get all non-system collection names from the database, this excludes indexes.
-
#collections ⇒ Array<Collection>
Get all non-system collections from the database.
-
#command(command) ⇒ Hash
Run
command
on the database. -
#drop ⇒ nil
Drop the database.
-
#initialize(session, name) ⇒ Database
constructor
Initialize the database.
-
#login(username, password) ⇒ Object
Log in with
username
andpassword
on the current database. -
#logout ⇒ Object
Log out from the current database.
Constructor Details
#initialize(session, name) ⇒ Database
Initialize the database.
102 103 104 105 106 107 108 109 |
# File 'lib/moped/database.rb', line 102 def initialize(session, name) if match = name.match(/\.|\s|\$|\\|\/|\x00/) raise Errors::InvalidDatabaseName.new( "Database names may not contain the character '#{match[0]}'." ) end @session, @name = session, name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/moped/database.rb', line 19 def name @name end |
#name The name of the database.(Thenameofthedatabase.) ⇒ Object (readonly)
19 |
# File 'lib/moped/database.rb', line 19 attr_reader :name, :session |
#session ⇒ Object (readonly)
Returns the value of attribute session.
19 20 21 |
# File 'lib/moped/database.rb', line 19 def session @session end |
#session The session.(Thesession.) ⇒ Object (readonly)
19 |
# File 'lib/moped/database.rb', line 19 attr_reader :name, :session |
Instance Method Details
#[](collection) ⇒ Collection
Get a collection by the provided name.
31 32 33 |
# File 'lib/moped/database.rb', line 31 def [](collection) Collection.new(self, collection) end |
#collection_names ⇒ Array<String>
Get all non-system collection names from the database, this excludes indexes.
56 57 58 59 60 61 62 |
# File 'lib/moped/database.rb', line 56 def collection_names namespaces = Collection.new(self, "system.namespaces").find(name: { "$not" => /#{name}\.system\.|\$/ }) namespaces.map do |doc| _name = doc["name"] _name[name.length + 1, _name.length] end end |
#collections ⇒ Array<Collection>
Get all non-system collections from the database.
43 44 45 |
# File 'lib/moped/database.rb', line 43 def collections collection_names.map{|name| Collection.new(self, name)} end |
#command(command) ⇒ Hash
Run command
on the database.
75 76 77 |
# File 'lib/moped/database.rb', line 75 def command(command) session.context.command(name.to_s, command) end |
#drop ⇒ nil
Drop the database.
87 88 89 90 91 |
# File 'lib/moped/database.rb', line 87 def drop session.with(consistency: :strong) do |session| session.context.command(name, dropDatabase: 1) end end |
#login(username, password) ⇒ Object
Log in with username
and password
on the current database.
120 121 122 |
# File 'lib/moped/database.rb', line 120 def login(username, password) session.context.login(name, username, password) end |
#logout ⇒ Object
Log out from the current database.
130 131 132 |
# File 'lib/moped/database.rb', line 130 def logout session.context.logout(name) end |