Class: Moped::Database
- Inherits:
-
Object
- Object
- Moped::Database
- Includes:
- Readable
- Defined in:
- lib/moped/database.rb
Overview
The class for interacting with a MongoDB database. One only interacts with this class indirectly through a session.
Instance Attribute Summary collapse
-
#name ⇒ String
The name of the database.
- #session ⇒ Object
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.
100 101 102 103 |
# File 'lib/moped/database.rb', line 100 def initialize(session, name) @session = session @name = name.to_s end |
Instance Attribute Details
#name ⇒ String
Returns The name of the database.
15 16 17 |
# File 'lib/moped/database.rb', line 15 def name @name end |
#session ⇒ Object
15 |
# File 'lib/moped/database.rb', line 15 attr_reader :name, :session |
Instance Method Details
#[](collection) ⇒ Collection
Get a collection by the provided name.
27 28 29 |
# File 'lib/moped/database.rb', line 27 def [](collection) Collection.new(self, collection) end |
#collection_names ⇒ Array<String>
Get all non-system collection names from the database, this excludes indexes.
52 53 54 55 56 57 58 |
# File 'lib/moped/database.rb', line 52 def collection_names namespaces = 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.
39 40 41 |
# File 'lib/moped/database.rb', line 39 def collections collection_names.map{ |name| Collection.new(self, name) } end |
#command(command) ⇒ Hash
Run command
on the database.
71 72 73 74 75 |
# File 'lib/moped/database.rb', line 71 def command(command) read_preference.with_node(cluster) do |node| node.command(name, command, ({})) end end |
#drop ⇒ nil
Drop the database.
85 86 87 88 89 |
# File 'lib/moped/database.rb', line 85 def drop session.with(read: :primary) do |session| session.command(dropDatabase: 1) end end |
#login(username, password) ⇒ Object
Log in with username
and password
on the current database.
114 115 116 |
# File 'lib/moped/database.rb', line 114 def login(username, password) cluster.add_credential(name, username, password) end |
#logout ⇒ Object
Log out from the current database.
124 125 126 |
# File 'lib/moped/database.rb', line 124 def logout cluster.delete_credential(name) end |