meteor/meteor · error · MiniMongoQueryError

argument to $type is not a number or a string

Error message

argument to $type is not a number or a string

What it means

Thrown by the $type operator when the operand is neither a string (alias) nor a number (BSON code). $type only accepts those two operand kinds; anything else (boolean, array, object, null, undefined) is rejected before any alias/code lookup.

Source

Thrown at packages/minimongo/common.js:126

          'javascriptWithScope': 15,
          'int': 16,
          'timestamp': 17,
          'long': 18,
          'decimal': 19,
          'minKey': -1,
          'maxKey': 127,
        };
        if (!hasOwn.call(operandAliasMap, operand)) {
          throw new MiniMongoQueryError(`unknown string alias for $type: ${operand}`);
        }
        operand = operandAliasMap[operand];
      } else if (typeof operand === 'number') {
        if (operand === 0 || operand < -1
          || (operand > 19 && operand !== 127)) {
          throw new MiniMongoQueryError(`Invalid numerical $type code: ${operand}`);
        }
      } else {
        throw new MiniMongoQueryError('argument to $type is not a number or a string');
      }

      return value => (
        value !== undefined && LocalCollection._f._type(value) === operand
      );
    },
  },
  $bitsAllSet: {
    compileElementSelector(operand) {
      const mask = getOperandBitmask(operand, '$bitsAllSet');
      return value => {
        const bitmask = getValueBitmask(value, mask.length);
        return bitmask && mask.every((byte, i) => (bitmask[i] & byte) === byte);
      };
    },
  },
  $bitsAnySet: {
    compileElementSelector(operand) {

View on GitHub (pinned to 5076d2f818)

Solutions

  1. Provide a string alias or numeric code: {field: {$type: 'string'}}.
  2. If the operand is optional, default it before building the selector.
  3. Validate the operand is string|number before constructing the query.

Example fix

// before
collection.find({ value: { $type: true } });
// after
collection.find({ value: { $type: 'bool' } });
Defensive patterns

Strategy: type-guard

Validate before calling

function typeSelector(operand) {
  if (typeof operand !== 'string' && typeof operand !== 'number') {
    throw new TypeError('$type operand must be string alias or number code');
  }
  return { $type: operand };
}

Type guard

function isValidTypeOperand(op) {
  return typeof op === 'string' || typeof op === 'number';
}

Prevention

When it happens

Trigger: Using {field: {$type: true}}, {field: {$type: null}}, {field: {$type: [1]}}, or {field: {$type: {}}} in a minimongo query.

Common situations: Passing a boolean flag; passing null from an optional config; wrapping the code in an array; passing an object that was meant for a different operator.

Related errors


AI-assisted analysis of meteor/meteor@5076d2f818 (2026-08-13). Data as JSON: /api/errors/80e502459206f5f8. Report an issue: GitHub.