litedb-org/LiteDB · error · LiteException

0

0

Error message

Collection ${this.Name} requires string as 'filename' or a document field 'filename'

What it means

Thrown by SysFileCsv.Input when reading from $file_csv and the 'filename' option is missing or not a string. GetOption(options,"filename")?.AsString returns null when absent/non-string, triggering the throw before opening the FileStream.

Source

Thrown at LiteDB/Engine/SystemCollections/SysFileCsv.cs:21

using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using static LiteDB.Constants;

namespace LiteDB.Engine
{
    internal class SysFileCsv : SystemCollection
    {
        private readonly static IFormatProvider _numberFormat = CultureInfo.InvariantCulture.NumberFormat;

        public SysFileCsv() : base("$file_csv")
        {
        }

        public override IEnumerable<BsonDocument> Input(BsonValue options)
        {
            var filename = GetOption(options, "filename")?.AsString ?? throw new LiteException(0, $"Collection ${this.Name} requires string as 'filename' or a document field 'filename'");
            var encoding = GetOption(options, "encoding", "utf-8").AsString;
            var delimiter = GetOption(options, "delimiter", ",").AsString[0];

            // read header (or first line as header)
            var header = new List<string>();

            if (options.IsDocument && options["header"].IsArray)
            {
                header.AddRange(options["header"].AsArray.Select(x => x.AsString));
            }

            using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var reader = new StreamReader(fs, Encoding.GetEncoding(encoding)))
                {
                    // if not header declared, use first line as header fields
                    if (header.Count == 0)
                    {

View on GitHub (pinned to f906a5f850)

Solutions

  1. Pass the filename: $file_csv('data.csv') or { filename: 'data.csv' }.
  2. Prefer $file('data.csv') which routes by extension and is less error-prone.
  3. Validate the filename option exists as a string before running the query.

Example fix

// before
db.Execute("SELECT * FROM $file_csv()"); // throws

// after
db.Execute("SELECT * FROM $file_csv('data.csv')");
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(filename)) throw new ArgumentException("filename required");
db.Execute($"SELECT * FROM $file_csv('{filename}')");

Try / catch

try { db.Execute($"SELECT * FROM $file_csv('{filename}')"); }
catch (LiteException ex) when (ex.Message.Contains("requires string as 'filename'")) {
    // obtain filename and retry
}

Prevention

When it happens

Trigger: SELECT * FROM $file_csv() with no filename, or $file_csv({ encoding:'utf-8' }) without a filename field, or passing filename as a non-string value.

Common situations: Using $file_csv directly (bypassing the $file router) and forgetting the filename; programmatically building options and omitting it.

Related errors


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