{"record":{"id":"e1b24be9363d8b31","repo":"mongodb/mongoid","slug":"regex-requires-a-regular-expression-argument-e","errorCode":null,"errorMessage":"$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}","messagePattern":"\\$regex requires a regular expression argument: #(.+?)","errorType":"exception","errorClass":"Mongoid::Errors::InvalidQuery","httpStatus":null,"severity":"error","filePath":"lib/mongoid/matcher/regex.rb","lineNumber":27,"sourceCode":"      # Returns whether a value satisfies a $regex expression.\n      #\n      # @param [ true | false ] exists Not used.\n      # @param [ String | Array<String> ] value The value to check.\n      # @param [ Regexp | BSON::Regexp::Raw ] condition The $regex condition.\n      #\n      # @return [ true | false ] Whether the value matches.\n      #\n      # @api private\n      module_function def matches?(_exists, value, condition)\n        condition = case condition\n                    when Regexp\n                      condition\n                    when BSON::Regexp::Raw\n                      condition.compile\n                    else\n                      # Note that strings must have been converted to a regular expression\n                      # instance already (with $options taken into account, if provided).\n                      raise Errors::InvalidQuery, \"$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}\"\n                    end\n\n        case value\n        when Array\n          value.any? do |v|\n            v =~ condition\n          end\n        when String\n          value =~ condition\n        else\n          false\n        end\n      end\n\n      # Returns whether an scalar or array value matches a Regexp.\n      #\n      # @param [ true | false ] exists Not used.\n      # @param [ String | Array<String> ] value The value to check.","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/matcher/regex.rb#L9-L45","documentation":"Error \"$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/matcher/regex.rb:27 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"}