mongodb/mongoid · error · NotImplementedError

Ruby does not allow same symbol operator with different…

Error message

Ruby does not allow same symbol operator with different values

What it means

While merging query expressions, _mongoid_expand_keys found the same operator applied to one field twice with different values (e.g. conflicting $gt ranges). Ruby hashes cannot hold two values for one operator key, so the contradictory condition is rejected.

Solutions

  1. Remove the duplicate/conflicting operator condition for the same field
  2. Combine the conditions into a single range or use any_of
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/criteria/queryable/mergeable.rb:273 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/e9ae710ffba02268. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/criteria/queryable/mergeable.rb:273

        private def _mongoid_expand_keys(expr)
          raise ArgumentError, 'Argument must be a Hash' unless expr.is_a?(Hash)

          result = BSON::Document.new
          expr.each do |field, value|
            field.__expr_part__(value.__expand_complex__, negating?).each do |k, v|
              if existing = result[k]
                if existing.is_a?(Hash)
                  # Existing value is an operator.
                  # If new value is also an operator, ensure there are no
                  # conflicts and add
                  if v.is_a?(Hash)
                    # The new value is also an operator.
                    # If there are no conflicts, combine the hashes, otherwise
                    # add new conditions to top level with $and.
                    if (v.keys & existing.keys).empty?
                      existing.update(v)
                    else
                      raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
                      result['$and'] ||= []
                      result['$and'] << { k => v }
                    end
                  else
                    # The new value is a simple value.
                    # Transform the implicit equality to either $eq or $regexp
                    # depending on the type of the argument. See
                    # https://www.mongodb.com/docs/manual/reference/operator/query/eq/#std-label-eq-usage-examples
                    # for the description of relevant server behavior.
                    op = case v
                         when Regexp, BSON::Regexp::Raw
                           '$regex'
                         else
                           '$eq'
                         end
                    # If there isn't an $eq/$regex operator already in the
                    # query, transform the new value into an operator
                    # expression and add it to the existing hash. Otherwise

View on GitHub (pinned to 0da0e23d71)