{"record":{"id":"6961daa1f407ac4f","repo":"litedb-org/LiteDB","slug":"between-expression-need-an-array-with-2-values","errorCode":null,"errorMessage":"BETWEEN expression need an array with 2 values","messagePattern":"BETWEEN expression need an array with 2 values","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"LiteDB/Document/Expression/Parser/BsonExpressionOperators.cs","lineNumber":178,"sourceCode":"            if (left.IsString && right.IsString)\n            {\n                return left.AsString.SqlLike(right.AsString, collation);\n            }\n            else\n            {\n                return false;\n            }\n        }\n\n        public static BsonValue LIKE_ANY(Collation collation, IEnumerable<BsonValue> left, BsonValue right) => left.Any(x => LIKE(collation, x, right));\n        public static BsonValue LIKE_ALL(Collation collation, IEnumerable<BsonValue> left, BsonValue right) => left.All(x => LIKE(collation, x, right));\n\n        /// <summary>\n        /// Test if left is between right-array. Returns true or false. Right value must be an array. Support multiple values\n        /// </summary>\n        public static BsonValue BETWEEN(Collation collation, BsonValue left, BsonValue right)\n        {\n            if (!right.IsArray) throw new InvalidOperationException(\"BETWEEN expression need an array with 2 values\");\n\n            var arr = right.AsArray;\n\n            if (arr.Count != 2) throw new InvalidOperationException(\"BETWEEN expression need an array with 2 values\");\n\n            var start = arr[0];\n            var end = arr[1];\n\n            //return left >= start && right <= end;\n            return collation.Compare(left, start) >= 0 && collation.Compare(left, end) <= 0;\n        }\n\n        public static BsonValue BETWEEN_ANY(Collation collation, IEnumerable<BsonValue> left, BsonValue right) => left.Any(x => BETWEEN(collation, x, right));\n        public static BsonValue BETWEEN_ALL(Collation collation, IEnumerable<BsonValue> left, BsonValue right) => left.All(x => BETWEEN(collation, x, right));\n\n        /// <summary>\n        /// 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\n        /// </summary>","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Document/Expression/Parser/BsonExpressionOperators.cs#L160-L196","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use the SQL-style syntax `field BETWEEN low AND high` so the parser builds the two-element array for you.","If calling BETWEEN programmatically, ensure the right operand is a BsonArray: `new BsonArray(new[] { start, end })`.","Validate the right operand with `.IsArray` before invoking the operator."],"exampleFix":"// before\nvar result = BsonExpressionOperators.BETWEEN(coll, field, new BsonValue(10));\n// after\nvar result = BsonExpressionOperators.BETWEEN(coll, field, new BsonArray(new BsonValue[] { 10, 20 }));","handlingStrategy":"validation","validationCode":"if (!right.IsArray)\n    throw new InvalidOperationException(\"BETWEEN requires an array right operand.\");","typeGuard":"static bool IsValidBetweenRight(BsonValue right) => right != null && right.IsArray;","tryCatchPattern":null,"preventionTips":["Use the SQL BETWEEN ... AND ... syntax so the parser constructs the array.","When calling BETWEEN programmatically, always pass a BsonArray.","Validate operand types before invoking expression operator functions directly."],"tags":["expression","between","query","invalid-operation"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}