{"record":{"id":"44d6967b5a1221a2","repo":"mongodb/mongoid","slug":"regex-is-required-if-options-is-given-errors","errorCode":null,"errorMessage":"$regex is required if $options is given: #{Errors::InvalidQuery.truncate_expr(condition)}","messagePattern":"\\$regex is required if \\$options is given: #(.+?)","errorType":"exception","errorClass":"Mongoid::Errors::InvalidQuery","httpStatus":null,"severity":"error","filePath":"lib/mongoid/matcher/field_expression.rb","lineNumber":25,"sourceCode":"    # @api private\n    module FieldExpression\n      # Returns whether a value satisfies a condition.\n      #\n      # @param [ true | false ] exists Whether the value exists.\n      # @param [ Object ] value The value to check.\n      # @param [ Hash | Object ] condition The condition predicate.\n      #\n      # @return [ true | false ] Whether the value matches.\n      #\n      # @api private\n      module_function def matches?(exists, value, condition)\n        if condition.is_a?(Hash)\n          condition.all? do |k, cond_v|\n            k = k.to_s\n            if k.start_with?('$')\n              if %w[$regex $options].include?(k)\n                unless condition.key?('$regex')\n                  raise Errors::InvalidQuery, \"$regex is required if $options is given: #{Errors::InvalidQuery.truncate_expr(condition)}\"\n                end\n\n                if k == '$regex'\n                  if options = condition['$options']\n                    cond_v = case cond_v\n                             when Regexp\n                               BSON::Regexp::Raw.new(cond_v.source, options)\n                             when BSON::Regexp::Raw\n                               BSON::Regexp::Raw.new(cond_v.pattern, options)\n                             else\n                               BSON::Regexp::Raw.new(cond_v, options)\n                             end\n                  elsif cond_v.is_a?(String)\n                    cond_v = BSON::Regexp::Raw.new(cond_v)\n                  end\n\n                  FieldOperator.get(k).matches?(exists, value, cond_v)\n                else","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/matcher/field_expression.rb#L7-L43","documentation":"Error \"$regex is required if $options is given: #{Errors::InvalidQuery.truncate_expr(condition)}\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/matcher/field_expression.rb:25 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"}