{"record":{"id":"5b6002c2c83136c7","repo":"litedb-org/LiteDB","slug":"0-5b6002","errorCode":"0","errorMessage":"WHERE filter can not use `*` expression in `{predicate.Source}`","messagePattern":"WHERE filter can not use `\\*` expression in `(.+?)`","errorType":"exception","errorClass":"LiteException","httpStatus":null,"severity":"error","filePath":"LiteDB/Engine/Query/QueryOptimization.cs","lineNumber":85,"sourceCode":"            // define IncludeBefore + IncludeAfter\n            this.DefineIncludes();\n\n            return _queryPlan;\n        }\n\n        #region Split Where\n\n        /// <summary>\n        /// Fill terms from where predicate list\n        /// </summary>\n        private void SplitWherePredicateInTerms()\n        {\n            void add(BsonExpression predicate)\n            {\n                // do not accept source * in WHERE\n                if (predicate.UseSource)\n                {\n                    throw new LiteException(0, $\"WHERE filter can not use `*` expression in `{predicate.Source}\");\n                }\n\n                // add expression in where list breaking AND statments\n                if (predicate.IsPredicate || predicate.Type == BsonExpressionType.Or)\n                {\n                    _terms.Add(predicate);\n                }\n                else if (predicate.Type == BsonExpressionType.And)\n                {\n                    var left = predicate.Left;\n                    var right = predicate.Right;\n\n                    add(left);\n                    add(right);\n                }\n                else\n                {\n                    throw LiteException.InvalidExpressionTypePredicate(predicate);","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Engine/Query/QueryOptimization.cs#L67-L103","documentation":"Thrown by QueryOptimization.SplitWherePredicateInTerms when a WHERE predicate expression has UseSource == true, meaning it references the root document source token ('*'). LiteDB's WHERE clause filters individual documents field-by-field; a '*' source has no meaningful filter semantics and cannot be indexed. The optimizer rejects it during query planning.","triggerScenarios":"Constructing a query whose WHERE expression uses the '*' / source wildcard, e.g. via BsonExpression.Create(\"* > 0\") used as a Where clause, or SQL like 'SELECT $ FROM col WHERE * = ...'. Any predicate where predicate.UseSource is true in the Where list triggers the throw.","commonSituations":"Dynamically building BsonExpression filters and accidentally passing a root-source expression; misusing the '*' syntax (which is valid in SELECT but not WHERE); copy-paste from a SELECT template into a WHERE context.","solutions":["Rewrite the WHERE predicate to reference a concrete field path (e.g. '$.field > 0') instead of '*'.","If you need to test the whole document, target a specific field or use a function-based expression on a named path.","Validate BsonExpression.UseSource is false before adding it to a Where list.","Keep '*' usage limited to the SELECT clause where it is supported."],"exampleFix":"// before — '*' in WHERE\ndb.Execute(\"SELECT $ FROM items WHERE * > 0\"); // throws\n// or in code:\nvar expr = BsonExpression.Create(\"* > 0\");\nquery.Where.Add(expr); // throws at optimization\n\n// after — reference a concrete field\ndb.Execute(\"SELECT $ FROM items WHERE $.value > 0\");","handlingStrategy":"validation","validationCode":"public void AddWhereClause(Query query, BsonExpression predicate)\n{\n    if (predicate.UseSource)\n        throw new ArgumentException($\"WHERE predicate '{predicate.Source}' uses the root source '*'. Rewrite it to reference a concrete field.\");\n    query.Where.Add(predicate);\n}","typeGuard":"static bool IsSafeWherePredicate(BsonExpression predicate) =>\n    predicate != null && !predicate.UseSource;","tryCatchPattern":"try\n{\n    db.Execute(sql);\n}\ncatch (LiteException ex) when (ex.Message.Contains(\"WHERE filter can not use `*`\"))\n{\n    throw new ArgumentException(\"The WHERE clause must reference concrete fields, not the root '*' source.\", ex);\n}","preventionTips":["Never use '*' in a WHERE expression; reference field paths like '$.field'.","Validate BsonExpression.UseSource is false before adding to a Where list.","Reserve '*' for SELECT clauses only.","Review dynamically-built expressions for accidental source references."],"tags":["query","where","bson-expression","litedb-engine"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}