mongodb/mongoid · error · ArgumentError

Field must be a string: #

Error message

Field must be a string: #{field}

What it means

Raised by Storable#add_one_expression when the field argument passed for a single-expression addition (part of the low-level query builder) is not a String. This API accepts an arbitrary field name or operator expression, but both must be given as strings; the faulting input is the non-String field value.

Solutions

  1. Convert the field name to a String before calling the storable API
  2. Prefer the public query builder methods, which normalize keys themselves
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at lib/mongoid/criteria/queryable/storable.rb:208 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/57dbecc933dfddbe. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/criteria/queryable/storable.rb:208

          else
            selector.store(operator, op_expr)
          end

          self
        end

        # Adds an arbitrary expression to the query.
        #
        # Field can either be a field name or an operator.
        #
        # Mutates the receiver.
        #
        # @param [ String ] field Field name or operator name.
        # @param [ Object ] value Field value or operator expression.
        #
        # @return [ Storable ] self.
        def add_one_expression(field, value)
          raise ArgumentError, "Field must be a string: #{field}" unless field.is_a?(String)

          if field.start_with?('$')
            add_operator_expression(field, value)
          else
            add_field_expression(field, value)
          end
        end
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)