{"record":{"id":"bda31a532df06c47","repo":"litedb-org/LiteDB","slug":"0-bda31a","errorCode":"0","errorMessage":"Field '{name}' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.","messagePattern":"Field '(.+?)' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause\\.","errorType":"exception","errorClass":"LiteException","httpStatus":null,"severity":"error","filePath":"LiteDB/Document/Expression/Parser/BsonExpressionOperators.cs","lineNumber":251,"sourceCode":"            {\n                return item;\n            }\n            else\n            {\n                return BsonValue.Null;\n            }\n        }\n\n        /// <summary>\n        /// Return a value from a value as document. If has no name, just return values ($). If value are not a document, do not return anything\n        /// </summary>\n        public static BsonValue MEMBER_PATH(BsonValue value, string name)\n        {\n            // if value is null is because there is no \"current document\", only \"all documents\"\n            // SELECT COUNT(*), $.pageID FROM $page_list IS invalid!\n            if (value == null)\n            {\n                throw new LiteException(0, $\"Field '{name}' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.\");\n            }\n\n            if (string.IsNullOrEmpty(name))\n            {\n                return value;\n            }\n            else if (value.IsDocument)\n            {\n                var doc = value.AsDocument;\n\n                if (doc.TryGetValue(name, out BsonValue item))\n                {\n                    return item;\n                }\n            }\n\n            return BsonValue.Null;\n        }","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Document/Expression/Parser/BsonExpressionOperators.cs#L233-L269","documentation":"Thrown by the MEMBER_PATH operator when the current value is null, which means there is no 'current document' context. This happens in aggregation queries where a bare field reference (like $.pageID) is used in the select list without a GROUP BY or aggregate function. LiteDB enforces SQL-like semantics: non-aggregated fields must appear in GROUP BY. This is a LiteException with code 0.","triggerScenarios":"Running an aggregation query like `SELECT COUNT(*), $.pageID FROM collection` where $.pageID is not inside an aggregate and there is no GROUP BY. Using a field path in a select list that mixes aggregate and non-aggregate columns incorrectly.","commonSituations":"Writing SQL-like aggregation queries against LiteDB collections where the user expects implicit grouping. Mixing COUNT(*) with bare field references. Migrating SQL queries to LiteDB without adjusting aggregation syntax.","solutions":["Wrap the bare field in an aggregate function, e.g., `SELECT COUNT(*), MAX($.pageID) FROM collection`.","Add a GROUP BY clause for the non-aggregated field: `SELECT $.pageID, COUNT(*) FROM collection GROUP BY $.pageID`.","Remove the non-aggregated field from the select list if it is not needed in the aggregate result."],"exampleFix":"// before\nvar results = db.GetCollection(\"docs\").Query()\n    .Select(\"COUNT(*), $.pageID\").ToList();\n// after\nvar results = db.GetCollection(\"docs\").Query()\n    .Select(\"COUNT(*), MAX($.pageID)\").ToList();","handlingStrategy":"validation","validationCode":"// Before running, validate that select-list fields are either aggregated or in GROUP BY\n// Example: prefer explicit aggregates\nvar sql = \"SELECT $.pageID, COUNT(*) FROM col GROUP BY $.pageID\";","typeGuard":null,"tryCatchPattern":"try { col.Query().Select(expr).ToList(); }\ncatch (LiteException ex) when (ex.Message.Contains(\"not contained in either an aggregate\"))\n{ /* fix select list to use aggregates or GROUP BY */ }","preventionTips":["In aggregation queries, wrap every non-grouped field in an aggregate function.","Add GROUP BY for fields that should appear unaggregated in the select list.","Review SQL-to-LiteDB query migrations for aggregate semantics."],"tags":["expression","aggregation","group-by","query","lite-exception"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}