Class: Moped::Indexes

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/moped/indexes.rb

Overview

Defines behaviour around indexes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, collection_name) ⇒ Indexes

Initialize the indexes.

Examples:

Create the new indexes.

Indexes.new(database, :artists)

Parameters:

  • database (Database)

    The database.

  • collection_name (String, Symbol)

    The name of the collection.

Since:

  • 1.0.0



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

def initialize(database, collection_name)
  @database, @collection_name = database, collection_name
  @namespace = "#{database.name}.#{collection_name}"
end

Instance Attribute Details

#collection_nameObject (readonly)

Returns the value of attribute collection_name.



10
11
12
# File 'lib/moped/indexes.rb', line 10

def collection_name
  @collection_name
end

#collection_name The collection name.(Thecollectionname.) ⇒ Object (readonly)



10
# File 'lib/moped/indexes.rb', line 10

attr_reader :collection_name, :database, :namespace

#databaseObject (readonly)

Returns the value of attribute database.



10
11
12
# File 'lib/moped/indexes.rb', line 10

def database
  @database
end

#database The database.(Thedatabase.) ⇒ Object (readonly)



10
# File 'lib/moped/indexes.rb', line 10

attr_reader :collection_name, :database, :namespace

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/moped/indexes.rb', line 10

def namespace
  @namespace
end

#namespace The index namespace.(Theindexnamespace.) ⇒ Object (readonly)



10
# File 'lib/moped/indexes.rb', line 10

attr_reader :collection_name, :database, :namespace

Instance Method Details

#[](key) ⇒ Hash?

Retrive an index by its definition.

Examples:

Get the index.

session[:users].indexes[id: 1]
# => {"v"=>1, "key"=>{"_id"=>1}, "ns"=>"moped_test.users", "name"=>"_id_" }

Parameters:

  • key (Hash)

    The index definition.

Returns:

  • (Hash, nil)

    The index with the provided key, or nil.

Since:

  • 1.0.0



23
24
25
# File 'lib/moped/indexes.rb', line 23

def [](key)
  database[:"system.indexes"].find(ns: namespace, key: key).one
end

#create(key, options = {}) ⇒ Hash

Create an index unless it already exists.

Examples:

Without options

session[:users].indexes.create(name: 1)
session[:users].indexes[name: 1]
# => {"v"=>1, "key"=>{"name"=>1}, "ns"=>"moped_test.users", "name"=>"name_1" }

With options

session[:users].indexes.create(
  { location: "2d", name: 1 },
  { unique: true, dropDups: true }
)
session[:users][location: "2d", name: 1]
{"v"=>1,
  "key"=>{"location"=>"2d", "name"=>1},
  "unique"=>true,
  "ns"=>"moped_test.users",
  "dropDups"=>true,
  "name"=>"location_2d_name_1"}

Parameters:

  • key (Hash)

    The index spec.

  • options (Hash) (defaults to: {})

    The options for the index.

Returns:

  • (Hash)

    The index spec.

See Also:

Since:

  • 1.0.0



55
56
57
58
59
# File 'lib/moped/indexes.rb', line 55

def create(key, options = {})
  spec = options.merge(ns: namespace, key: key)
  spec[:name] ||= key.to_a.join("_")
  database[:"system.indexes"].insert(spec)
end

#drop(key = nil) ⇒ Boolean

Drop an index, or all indexes.

Examples:

Drop all indexes

session[:users].indexes.count # => 3
# Does not drop the _id index
session[:users].indexes.drop
session[:users].indexes.count # => 1

Drop a particular index

session[:users].indexes.drop(name: 1)
session[:users].indexes[name: 1] # => nil

Parameters:

  • key (Hash) (defaults to: nil)

    The index’s key.

Returns:

  • (Boolean)

    Whether the indexes were dropped.

Since:

  • 1.0.0



78
79
80
81
82
83
84
85
86
87
# File 'lib/moped/indexes.rb', line 78

def drop(key = nil)
  if key
    index = self[key] or return false
    name = index["name"]
  else
    name = "*"
  end
  result = database.command deleteIndexes: collection_name, index: name
  result["ok"] == 1
end

#each {|Hash| ... } ⇒ Enumerator

Iterate over each of the indexes for the collection.

Examples:

Iterate over the indexes.

indexes.each do |spec|
  #...
end

Yields:

  • (Hash)

    Each index for the collection.

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 1.0.0



101
102
103
# File 'lib/moped/indexes.rb', line 101

def each(&block)
  database[:"system.indexes"].find(ns: namespace).each(&block)
end