litedb-org/LiteDB · error · InvalidOperationException

BETWEEN expression need an array with 2 values

Error message

BETWEEN expression need an array with 2 values

What it means

Thrown by the BETWEEN operator when the right-hand operand is not an array. BETWEEN requires the right side to be a two-element array [start, end]; this guard rejects non-array types before attempting to read bounds. This is an InvalidOperationException thrown during expression evaluation.

Source

Thrown at LiteDB/Document/Expression/Parser/BsonExpressionOperators.cs:178

            if (left.IsString && right.IsString)
            {
                return left.AsString.SqlLike(right.AsString, collation);
            }
            else
            {
                return false;
            }
        }

        public static BsonValue LIKE_ANY(Collation collation, IEnumerable<BsonValue> left, BsonValue right) => left.Any(x => LIKE(collation, x, right));
        public static BsonValue LIKE_ALL(Collation collation, IEnumerable<BsonValue> left, BsonValue right) => left.All(x => LIKE(collation, x, right));

        /// <summary>
        /// Test if left is between right-array. Returns true or false. Right value must be an array. Support multiple values
        /// </summary>
        public static BsonValue BETWEEN(Collation collation, BsonValue left, BsonValue right)
        {
            if (!right.IsArray) throw new InvalidOperationException("BETWEEN expression need an array with 2 values");

            var arr = right.AsArray;

            if (arr.Count != 2) throw new InvalidOperationException("BETWEEN expression need an array with 2 values");

            var start = arr[0];
            var end = arr[1];

            //return left >= start && right <= end;
            return collation.Compare(left, start) >= 0 && collation.Compare(left, end) <= 0;
        }

        public static BsonValue BETWEEN_ANY(Collation collation, IEnumerable<BsonValue> left, BsonValue right) => left.Any(x => BETWEEN(collation, x, right));
        public static BsonValue BETWEEN_ALL(Collation collation, IEnumerable<BsonValue> left, BsonValue right) => left.All(x => BETWEEN(collation, x, right));

        /// <summary>
        /// Test if left are in any value in right side (when right side is an array). If right side is not an array, just implement a simple Equals (=). Returns true or false
        /// </summary>

View on GitHub (pinned to f906a5f850)

Solutions

  1. Use the SQL-style syntax `field BETWEEN low AND high` so the parser builds the two-element array for you.
  2. If calling BETWEEN programmatically, ensure the right operand is a BsonArray: `new BsonArray(new[] { start, end })`.
  3. Validate the right operand with `.IsArray` before invoking the operator.

Example fix

// before
var result = BsonExpressionOperators.BETWEEN(coll, field, new BsonValue(10));
// after
var result = BsonExpressionOperators.BETWEEN(coll, field, new BsonArray(new BsonValue[] { 10, 20 }));
Defensive patterns

Strategy: validation

Validate before calling

if (!right.IsArray)
    throw new InvalidOperationException("BETWEEN requires an array right operand.");

Type guard

static bool IsValidBetweenRight(BsonValue right) => right != null && right.IsArray;

Prevention

When it happens

Trigger: Using BETWEEN with a scalar right-hand side, e.g., `$.age BETWEEN 10` or `$.price BETWEEN $.min`. The parser constructs a two-element array for `x BETWEEN a AND b`, so this typically fires when BETWEEN is invoked programmatically or with a parameter/column that resolved to a non-array.

Common situations: Calling the BETWEEN operator function directly with a hand-built right value. An expression parameter that was expected to be an array resolves to a scalar or null at runtime. Query builder bugs that construct BETWEEN clauses incorrectly.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/6961daa1f407ac4f. Report an issue: GitHub.