Class: Moped::Database

Inherits:
Object
  • Object
show all
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.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, name) ⇒ Database

Initialize the database.

Examples:

Initialize a database object.

Database.new(session, :artists)

Parameters:

  • session (Session)

    The session.

  • name (String, Symbol)

    The name of the database.

Since:

  • 1.0.0



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

#nameString

Returns The name of the database.

Returns:

  • (String)

    The name of the database.



15
16
17
# File 'lib/moped/database.rb', line 15

def name
  @name
end

#sessionObject

Since:

  • 1.0.0



15
# File 'lib/moped/database.rb', line 15

attr_reader :name, :session

Instance Method Details

#[](collection) ⇒ Collection

Get a collection by the provided name.

Examples:

Get a collection.

session[:users]

Parameters:

  • collection (Symbol, String)

    The collection name.

Returns:

Since:

  • 1.0.0



27
28
29
# File 'lib/moped/database.rb', line 27

def [](collection)
  Collection.new(self, collection)
end

#collection_namesArray<String>

Get all non-system collection names from the database, this excludes indexes.

Examples:

Get all the collection names.

database.collection_names

Returns:

  • (Array<String>)

    The names of all collections.

Since:

  • 1.0.0



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

#collectionsArray<Collection>

Get all non-system collections from the database.

Examples:

Get all the collections.

database.collections

Returns:

Since:

  • 1.0.0



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.

Examples:

Run a command.

db.command(ismaster: 1)
# => { "master" => true, hosts: [] }

Parameters:

  • command (Hash)

    The command to run.

Returns:

  • (Hash)

    the result of the command.

Since:

  • 1.0.0



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, query_options({}))
  end
end

#dropnil

Drop the database.

Examples:

Drop the database.

database.drop

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



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.

Examples:

Authenticate against the database.

session.("user", "pass")

Parameters:

  • username (String)

    The username.

  • password (String)

    The password.

Since:

  • 1.0.0



114
115
116
# File 'lib/moped/database.rb', line 114

def (username, password)
  cluster.add_credential(name, username, password)
end

#logoutObject

Log out from the current database.

Examples:

Logout from the current database.

session.logout

Since:

  • 1.0.0



124
125
126
# File 'lib/moped/database.rb', line 124

def logout
  cluster.delete_credential(name)
end