mongodb/mongoid · error · Mongoid::Errors::InvalidIndex

Invalid index specification on

Error message

Invalid index specification on %{klass}: %{spec}, %{options}

What it means

Raised by the index specification validator when an index defined via index_in_collection (index macro) uses option keys that are not recognized. Each key/value pair of the options hash is checked against the allowed index options.

Solutions

  1. Remove or correct the invalid option key in the index definition
  2. Verify option names against the MongoDB driver's index options (e.g. :unique, :name, :sparse, :expire_after_seconds)
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/indexable/validators/options.rb:75 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/8d9b4277cb2c6541. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/indexable/validators/options.rb:75

        end

        private

        # Validates the options of the index spec.
        #
        # @api private
        #
        # @example Validate the options.
        #   Options.validate_options(Band, name: 1)
        #
        # @param [ Class ] klass The model class.
        # @param [ Hash ] spec The index specification.
        # @param [ Hash ] options The index options.
        #
        # @raise [ Errors::InvalidIndex ] If validation failed.
        def validate_options(klass, spec, options)
          options.each_pair do |name, _value|
            raise Errors::InvalidIndex.new(klass, spec, options) unless VALID_OPTIONS.include?(name)
          end
        end

        # Validates the index spec.
        #
        # @api private
        #
        # @example Validate the spec.
        #   Options.validate_spec(Band, name: 1)
        #
        # @param [ Class ] klass The model class.
        # @param [ Hash ] spec The index specification.
        # @param [ Hash ] options The index options.
        #
        # @raise [ Errors::InvalidIndex ] If validation failed.
        def validate_spec(klass, spec, options)
          raise Errors::InvalidIndex.new(klass, spec, options) unless spec.is_a?(::Hash)

View on GitHub (pinned to 0da0e23d71)