{"record":{"id":"15b8c5be481548d6","repo":"mongodb/mongoid","slug":"not-argument-cannot-be-an-empty-hash-errors-i","errorCode":null,"errorMessage":"$not argument cannot be an empty hash: #{Errors::InvalidQuery.truncate_expr(condition)}","messagePattern":"\\$not argument cannot be an empty hash: #(.+?)","errorType":"exception","errorClass":"Mongoid::Errors::InvalidQuery","httpStatus":null,"severity":"error","filePath":"lib/mongoid/matcher/not.rb","lineNumber":25,"sourceCode":"    # @api private\n    module Not\n      # Returns whether a value satisfies an $not expression.\n      #\n      # @param [ true | false ] exists Whether the value exists.\n      # @param [ Object ] value The value to check.\n      # @param [ Hash | Regexp | BSON::Regexp::Raw ] condition\n      #   The $not condition predicate.\n      #\n      # @return [ true | false ] Whether the value matches.\n      #\n      # @api private\n      module_function def matches?(exists, value, condition)\n        case condition\n        when ::Regexp, BSON::Regexp::Raw\n          !Regex.matches?(exists, value, condition)\n        when Hash\n          if condition.empty?\n            raise Errors::InvalidQuery, \"$not argument cannot be an empty hash: #{Errors::InvalidQuery.truncate_expr(condition)}\"\n          end\n\n          condition.all? do |(k, cond_v)|\n            k = k.to_s\n            unless k.start_with?('$')\n              raise Errors::InvalidQuery, \"$not arguments must be operators: #{Errors::InvalidQuery.truncate_expr(k)}\"\n            end\n\n            !FieldOperator.get(k).matches?(exists, value, cond_v)\n          end\n        else\n          raise Errors::InvalidQuery, \"$not argument must be a Hash or a regular expression: #{Errors::InvalidQuery.truncate_expr(condition)}\"\n        end\n      end\n    end\n  end\nend\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/matcher/not.rb#L7-L43","documentation":"Raised inside Mongoid::Matcher::Not.matches? when an $not condition is given as a Hash that contains no keys. The matcher dispatches on the condition type: a Regexp is negated directly, while a Hash is expected to hold one or more query operator entries (keys starting with '$') whose matches are negated. An empty hash is rejected by this guard because there are no operators to negate, which would make the match result ambiguous (vacuously true in Ruby's all? but semantically meaningless as a query), and the server itself rejects {} as an $not argument. The input at fault is the condition argument — a caller-written or programmatically built $not clause like {\"$not\" => {}}. Note this is a sibling guard to the ones rejecting non-operator keys and non-Hash/non-Regexp conditions in the same method.","triggerScenarios":"Thrown at lib/mongoid/matcher/not.rb:25 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Provide at least one operator condition inside the $not hash","Remove the $not clause if no negation is needed"],"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"}