{"record":{"id":"c476708000732bf9","repo":"mongodb/mongoid","slug":"full-shard-key-syntax-requires-1-or-2-arguments","errorCode":null,"errorMessage":"Full shard_key syntax requires 1 or 2 arguments","messagePattern":"Full shard_key syntax requires 1 or 2 arguments","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/mongoid/shardable.rb","lineNumber":129,"sourceCode":"      #     include Mongoid::Document\n      #     field :first_name, :type => String\n      #     field :last_name, :type => String\n      #\n      #     shard_key first_name: 1, last_name: 1\n      #   end\n      def shard_key(*args)\n        unless args.first.is_a?(Hash)\n          # Shorthand syntax\n          raise ArgumentError, 'Shorthand shard_key syntax does not permit options' if args.last.is_a?(Hash)\n\n          spec = Hash[args.map do |name|\n            [ name, 1 ]\n          end]\n\n          return shard_key(spec)\n        end\n\n        raise ArgumentError, 'Full shard_key syntax requires 1 or 2 arguments' if args.length > 2\n\n        spec, options = args\n\n        spec = Hash[spec.map do |name, value|\n          value = value.to_s if value.is_a?(Symbol)\n          [ database_field_name(name).to_sym, value ]\n        end]\n\n        self.shard_key_fields = spec.keys\n        self.shard_config = {\n          key: spec.freeze,\n          options: (options || {}).dup.freeze\n        }.freeze\n      end\n    end\n  end\nend\n","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/shardable.rb#L111-L147","documentation":"Raised by Document.shard_key when the full syntax is used with more or fewer than the supported argument count. After a leading Hash of shard key fields, Mongoid accepts at most an options Hash (one argument) or a tag array (two arguments); any other arity is rejected as an ArgumentError.","triggerScenarios":"Thrown at lib/mongoid/shardable.rb:129 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call shard_key with exactly the spec Hash plus at most one options Hash: shard_key({ a: 1, b: 1 }, { validate: false })","Use the shorthand field-list form for simple keys"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","contentChangedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}