Class: Moped::Database

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

Examples:

session.use :moped
session.drop
session[:users].insert(name: "John")
session.with(database: :moped) do |moped|
  moped[:users].drop
end

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:

Since:

  • 1.0.0



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

#nameObject (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

#sessionObject (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.

Examples:

Get a collection.

session[:users]

Parameters:

Returns:

Since:

  • 1.0.0



31
32
33
# File 'lib/moped/database.rb', line 31

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:

Since:

  • 1.0.0



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

#collectionsArray<Collection>

Get all non-system collections from the database.

Examples:

Get all the collections.

database.collections

Returns:

Since:

  • 1.0.0



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.

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



75
76
77
# File 'lib/moped/database.rb', line 75

def command(command)
  session.context.command(name.to_s, command)
end

#dropnil

Drop the database.

Examples:

Drop the database.

database.drop

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



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.

Examples:

Authenticate against the database.

session.("user", "pass")

Parameters:

  • username (String)

    The username.

  • password (String)

    The password.

Since:

  • 1.0.0



120
121
122
# File 'lib/moped/database.rb', line 120

def (username, password)
  session.context.(name, username, password)
end

#logoutObject

Log out from the current database.

Examples:

Logout from the current database.

session.logout

Since:

  • 1.0.0



130
131
132
# File 'lib/moped/database.rb', line 130

def logout
  session.context.logout(name)
end