mongodb/mongoid · error · ArgumentError

Shorthand shard_key syntax does not permit options

Error message

Shorthand shard_key syntax does not permit options

What it means

Error "Shorthand shard_key syntax does not permit options" thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/shardable.rb:120

      end
    end

    module ClassMethods
      # Specifies a shard key with the field(s) specified.
      #
      # @example Specify the shard key.
      #
      #   class Person
      #     include Mongoid::Document
      #     field :first_name, :type => String
      #     field :last_name, :type => String
      #
      #     shard_key first_name: 1, last_name: 1
      #   end
      def shard_key(*args)
        unless args.first.is_a?(Hash)
          # Shorthand syntax
          raise ArgumentError, 'Shorthand shard_key syntax does not permit options' if args.last.is_a?(Hash)

          spec = Hash[args.map do |name|
            [ name, 1 ]
          end]

          return shard_key(spec)
        end

        raise ArgumentError, 'Full shard_key syntax requires 1 or 2 arguments' if args.length > 2

        spec, options = args

        spec = Hash[spec.map do |name, value|
          value = value.to_s if value.is_a?(Symbol)
          [ database_field_name(name).to_sym, value ]
        end]

        self.shard_key_fields = spec.keys

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/shardable.rb:120 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/499bcf4196a13b60. Report an issue: GitHub.