Sha256: 4dcbb292fdf2e1212a01b7759aad1a57f730f406c02c39a936ddb7ba8ecaada2

Contents?: true

Size: 1.56 KB

Versions: 34

Compression:

Stored size: 1.56 KB

Contents

module MongoDoc
  module Index

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

    # 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.
    #
    # <tt>Person.index(:last_name)</tt>
    # <tt>Person.index(:ssn, :unique => true)</tt>
    # <tt>Person.index(:first_name => :asc, :last_name => :asc)</tt>
    # <tt>Person.index(:first_name => :asc, :last_name => :asc, :unique => true)</tt>
    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

    protected
    def to_mongo_direction(fields_hash)
      fields_hash.to_a.map {|field| [field.first, direction(field.last)]}
    end

    def direction(dir)
      DIRECTION[dir] || Mongo::ASCENDING
    end
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
mongo_doc-0.6.34 lib/mongo_doc/index.rb
mongo_doc-0.6.33 lib/mongo_doc/index.rb
mongo_doc-0.6.32 lib/mongo_doc/index.rb
mongo_doc-0.6.31 lib/mongo_doc/index.rb
mongo_doc-0.6.30 lib/mongo_doc/index.rb
mongo_doc-0.6.29 lib/mongo_doc/index.rb
mongo_doc-0.6.28 lib/mongo_doc/index.rb
mongo_doc-0.6.27 lib/mongo_doc/index.rb
mongo_doc-0.6.26 lib/mongo_doc/index.rb
mongo_doc-0.6.25 lib/mongo_doc/index.rb
mongo_doc-0.6.23 lib/mongo_doc/index.rb
mongo_doc-0.6.22 lib/mongo_doc/index.rb
mongo_doc-0.6.21 lib/mongo_doc/index.rb
mongo_doc-0.6.20 lib/mongo_doc/index.rb
mongo_doc-0.6.19 lib/mongo_doc/index.rb
mongo_doc-0.6.18 lib/mongo_doc/index.rb
mongo_doc-0.6.17 lib/mongo_doc/index.rb
mongo_doc-0.6.16 lib/mongo_doc/index.rb
mongo_doc-0.6.15 lib/mongo_doc/index.rb
mongo_doc-0.6.14 lib/mongo_doc/index.rb