Module: MongoDoc::Index

Defined in:
lib/mongo_doc/index.rb

Constant Summary collapse

DIRECTION =
{ :asc => Mongo::ASCENDING,
:desc => Mongo::DESCENDING,
:geo2d => Mongo::GEO2D }
OPTIONS =
[:min, :max, :background, :unique, :dropDups]

Instance Method Summary collapse

Instance Method Details

#index(*args) ⇒ Object

Create an index on a collection.

For compound indexes, pass pairs of fields and directions (:asc, :desc) as a hash.

For a unique index, pass the option :unique => true. To create the index in the background, pass the options :background => true. If you want to remove duplicates from existing records when creating the unique index, pass the option :dropDups => true

For GeoIndexing, specify the minimum and maximum longitude and latitude values with the :min and :max options.

Person.index(:last_name) Person.index(:ssn, :unique => true) Person.index(:first_name => :asc, :last_name => :asc) Person.index(:first_name => :asc, :last_name => :asc, :unique => true)



26
27
28
29
30
31
32
33
34
35
# File 'lib/mongo_doc/index.rb', line 26

def index(*args)
  options_and_fields = args.extract_options!
  if args.any?
    collection.create_index(args.first, options_and_fields)
  else
    fields = options_and_fields.except(*OPTIONS)
    options = options_and_fields.slice(*OPTIONS)
    collection.create_index(to_mongo_direction(fields), options)
  end
end