litedb-org/LiteDB · error · LiteException

0

0

Error message

{_name} do not support as output collection

What it means

Thrown by the virtual SystemCollection.Output when a system collection that does not override Output is used as an INSERT/export target. The base implementation always throws; only $file/$file_json/$file_csv implement Output. So INSERT INTO $<read-only-collection> is rejected.

Source

Thrown at LiteDB/Engine/SystemCollections/SystemCollection.cs:43

            : this(name)
        {
            _input = input;
        }

        /// <summary>
        /// Get system collection name (must starts with $)
        /// </summary>
        public string Name => _name;

        /// <summary>
        /// Get input data source factory
        /// </summary>
        public virtual IEnumerable<BsonDocument> Input(BsonValue options) => _input();

        /// <summary>
        /// Get output data source factory (must implement in inherit class)
        /// </summary>
        public virtual int Output(IEnumerable<BsonDocument> source, BsonValue options) => throw new LiteException(0, $"{_name} do not support as output collection");

        /// <summary>
        /// Static helper to read options arg as plain value or as document fields
        /// </summary>
        protected static BsonValue GetOption(BsonValue options, string key)
        {
            return GetOption(options, key, null);
        }

        /// <summary>
        /// Static helper to read options arg as plain value or as document fields
        /// </summary>
        protected static BsonValue GetOption(BsonValue options, string key, BsonValue defaultValue)
        {
            if (options != null && options.IsDocument)
            {
                if (options.AsDocument.TryGetValue(key, out var value))
                {

View on GitHub (pinned to f906a5f850)

Solutions

  1. Insert into a real user collection, not a system ($) collection.
  2. For file export use $file/$file_json/$file_csv which do support Output.
  3. Verify a system collection supports writes before targeting it with INSERT INTO.

Example fix

// before
db.Execute("INSERT INTO $collections({ name:'x' })"); // throws

// after
db.Execute("INSERT INTO collections ({ name:'x' })");
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<string> WritableSys = new() { "$file", "$file_json", "$file_csv" };
if (target.StartsWith("$") && !WritableSys.Contains(target))
    throw new ArgumentException($"{target} is read-only; insert into a real collection");

Type guard

static bool IsWritableCollection(string name) => !name.StartsWith("$") || name is "$file" or "$file_json" or "$file_csv";

Try / catch

try { db.Execute($"INSERT INTO {target} ..."); }
catch (LiteException ex) when (ex.Message.Contains("do not support as output collection")) {
    // redirect to a user collection
}

Prevention

When it happens

Trigger: INSERT INTO $collections SELECT ...; INSERT INTO $indexes; INSERT INTO $query; INSERT INTO any read-only system collection (those exposing only Input).

Common situations: Treating system collections as writable; attempting to populate metadata views; copy-pasting an export template and pointing it at a $-collection.

Related errors


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