{"record":{"id":"57193bde1bc24032","repo":"mongodb/mongoid","slug":"elemmatch-requires-a-hash-operand-errors-inva","errorCode":null,"errorMessage":"$elemMatch requires a Hash operand: #{Errors::InvalidQuery.truncate_expr(condition)}","messagePattern":"\\$elemMatch requires a Hash operand: #(.+?)","errorType":"exception","errorClass":"Mongoid::Errors::InvalidQuery","httpStatus":null,"severity":"error","filePath":"lib/mongoid/matcher/elem_match.rb","lineNumber":20,"sourceCode":"  module Matcher\n    # In-memory matcher for $elemMatch expression.\n    #\n    # @see https://www.mongodb.com/docs/manual/reference/operator/query/elemMatch/\n    #\n    # @api private\n    module ElemMatch\n      # Returns whether a value satisfies an $elemMatch expression.\n      #\n      # @param [ true | false ] exists Not used.\n      # @param [ Object | Array<Object> ] value The value to check.\n      # @param [ Hash ] expr The $elemMatch condition predicate.\n      #\n      # @return [ true | false ] Whether the value matches.\n      #\n      # @api private\n      module_function def matches?(_exists, value, condition)\n        unless condition.is_a?(Hash)\n          raise Errors::InvalidQuery, \"$elemMatch requires a Hash operand: #{Errors::InvalidQuery.truncate_expr(condition)}\"\n        end\n\n        if value.is_a?(Array) && !value.empty?\n          value.any? do |v|\n            ElemMatchExpression.matches?(v, condition)\n          end\n        else\n          # Validate the condition is valid, even though we will never attempt\n          # matching it.\n          condition.each do |k, _v|\n            k = k.to_s\n            next unless k.start_with?('$')\n\n            begin\n              ExpressionOperator.get(k)\n            rescue Mongoid::Errors::InvalidExpressionOperator\n              begin\n                FieldOperator.get(k)","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/matcher/elem_match.rb#L2-L38","documentation":"Error \"$elemMatch requires a Hash operand: #{Errors::InvalidQuery.truncate_expr(condition)}\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/matcher/elem_match.rb:20 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}