litedb-org/LiteDB · error · LiteException

0

0

Error message

Unknow file format in $file: `{format}`

What it means

Thrown by SysFile.Input when the requested file format is not registered. SysFile ($file) only registers 'json' and 'csv' (case-insensitive). The format is derived from an explicit 'format' option or, if absent, the filename extension (with the leading dot stripped). Any other extension/format string hits this.

Source

Thrown at LiteDB/Engine/SystemCollections/SysFile.cs:32

        {
            ["json"] = new SysFileJson(),
            ["csv"] = new SysFileCsv()
        };

        public SysFile() : base("$file")
        {
        }

        public override IEnumerable<BsonDocument> Input(BsonValue options)
        {
            var format = this.GetFormat(options);

            if (_formats.TryGetValue(format, out var factory))
            {
                return factory.Input(options);
            }

            throw new LiteException(0, $"Unknow file format in $file: `{format}`");
        }

        public override int Output(IEnumerable<BsonDocument> source, BsonValue options)
        {
            var format = this.GetFormat(options);

            if (_formats.TryGetValue(format, out var factory))
            {
                return factory.Output(source, options);
            }

            throw new LiteException(0, $"Unknow file format in $file: `{format}`");
        }

        private string GetFormat(BsonValue options)
        {
            var filename = GetOption(options, "filename")?.AsString ?? throw new LiteException(0, $"Collection $file requires string as 'filename' or a document field 'filename'");
            var format = GetOption(options, "format", Path.GetExtension(filename)).AsString;

View on GitHub (pinned to f906a5f850)

Solutions

  1. Convert the source to JSON or CSV first, then use $file('data.json') or $file('data.csv').
  2. Pass an explicit format option that equals json or csv: { filename:'data', format:'csv' }.
  3. For other formats, parse them in application code and insert documents directly.

Example fix

// before
db.Execute("SELECT * FROM $file('data.xml')"); // throws

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

Strategy: validation

Validate before calling

static readonly HashSet<string> Supported = new(StringComparer.OrdinalIgnoreCase) { "json", "csv" };
string fmt = Path.GetExtension(path)?.TrimStart('.');
if (!Supported.Contains(fmt)) throw new ArgumentException($"Unsupported $file format: {fmt}");
db.Execute($"SELECT * FROM $file('{path}')");

Type guard

static bool IsSupportedFileFormat(string path) {
    var ext = Path.GetExtension(path)?.TrimStart('.');
    return ext == "json" || ext == "csv";
}

Try / catch

try { db.Execute($"SELECT * FROM $file('{path}')"); }
catch (LiteException ex) when (ex.Message.Contains("Unknow file format")) {
    // convert to json/csv then retry
}

Prevention

When it happens

Trigger: Querying db.GetCollection("$file") / SELECT * FROM $file('data.xml') or $file('data.xlsx'), or passing { filename:'data.db', format:'db' }; anything whose resolved format is not json or csv.

Common situations: Trying to import XML/Excel/Parquet via $file (unsupported); passing a file with no extension and no explicit format option; typos in the format option like 'jSon' is fine but 'json ' (trailing space) or 'JSON5' is not.

Related errors


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