litedb-org/LiteDB · error · LiteException

0

0

Error message

Field '{name}' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

What it means

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.

Source

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

            {
                return item;
            }
            else
            {
                return BsonValue.Null;
            }
        }

        /// <summary>
        /// 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
        /// </summary>
        public static BsonValue MEMBER_PATH(BsonValue value, string name)
        {
            // if value is null is because there is no "current document", only "all documents"
            // SELECT COUNT(*), $.pageID FROM $page_list IS invalid!
            if (value == null)
            {
                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.");
            }

            if (string.IsNullOrEmpty(name))
            {
                return value;
            }
            else if (value.IsDocument)
            {
                var doc = value.AsDocument;

                if (doc.TryGetValue(name, out BsonValue item))
                {
                    return item;
                }
            }

            return BsonValue.Null;
        }

View on GitHub (pinned to f906a5f850)

Solutions

  1. Wrap the bare field in an aggregate function, e.g., `SELECT COUNT(*), MAX($.pageID) FROM collection`.
  2. Add a GROUP BY clause for the non-aggregated field: `SELECT $.pageID, COUNT(*) FROM collection GROUP BY $.pageID`.
  3. Remove the non-aggregated field from the select list if it is not needed in the aggregate result.

Example fix

// before
var results = db.GetCollection("docs").Query()
    .Select("COUNT(*), $.pageID").ToList();
// after
var results = db.GetCollection("docs").Query()
    .Select("COUNT(*), MAX($.pageID)").ToList();
Defensive patterns

Strategy: validation

Validate before calling

// Before running, validate that select-list fields are either aggregated or in GROUP BY
// Example: prefer explicit aggregates
var sql = "SELECT $.pageID, COUNT(*) FROM col GROUP BY $.pageID";

Try / catch

try { col.Query().Select(expr).ToList(); }
catch (LiteException ex) when (ex.Message.Contains("not contained in either an aggregate"))
{ /* fix select list to use aggregates or GROUP BY */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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