litedb-org/LiteDB · error · NotSupportedException

BSON type not supported

Error message

BSON type not supported

What it means

Thrown by the legacy BsonReader (used when importing/reading v4-era data files via FileReader) when it encounters a BSON type code byte it does not recognize or support. The reader handles Double(0x01), String(0x02), Document(0x03), Array(0x04), Binary(0x05), ObjectId(0x07), Boolean(0x08), DateTime(0x09), Null(0x0A), Int32(0x10), Int64(0x12), Decimal(0x13), MinKey(0xFF), MaxKey(0x7F). Any other byte triggers NotSupportedException.

Source

Thrown at LiteDB/Engine/FileReader/Legacy/BsonReader.cs:148

            }
            else if (type == 0x12) // Int64
            {
                return reader.ReadInt64();
            }
            else if (type == 0x13) // Decimal
            {
                return reader.ReadDecimal();
            }
            else if (type == 0xFF) // MinKey
            {
                return BsonValue.MinValue;
            }
            else if (type == 0x7F) // MaxKey
            {
                return BsonValue.MaxValue;
            }

            throw new NotSupportedException("BSON type not supported");
        }
    }
}

View on GitHub (pinned to f906a5f850)

Solutions

  1. Pre-process the source BSON to convert unsupported types to supported equivalents (e.g. regex to string, undefined to null).
  2. Use a different reader (the MongoDB C# driver or a custom BSON parser) that supports the wider type set, then insert documents into LiteDB.
  3. If the file is corrupt, restore from backup or skip the offending document.
  4. File a feature request / check the LiteDB version for expanded type support.

Example fix

// Not directly fixable in caller code — the legacy BsonReader has a fixed type table.
// Workaround: convert the source file first.
// before: reader.ReadFile("legacy.bson") throws on type 0x0B (Regex)
// after: pre-convert unsupported types using a full BSON parser, then write standard BSON:
//   foreach doc in sourceBson: replace Regex/Undefined/etc with String/Null, then feed to LiteDB Insert.
Defensive patterns

Strategy: try-catch

Validate before calling

// No pre-check available from the caller; the type table is internal to BsonReader.
// If importing BSON from MongoDB, pre-scan/convert unsupported types using the MongoDB driver before feeding to LiteDB.
public BsonDocument ConvertUnsupportedTypes(BsonDocument doc)
{
    foreach (var k in doc.Keys.ToList())
    {
        var v = doc[k];
        // example: convert any non-LiteDB-supported representation to string/null
        if (v == null) doc[k] = BsonValue.Null;
    }
    return doc;
}

Try / catch

try
{
    using var reader = new LegacyBsonReader(stream);
    // read documents...
}
catch (NotSupportedException ex) when (ex.Message.Contains("BSON type not supported"))
{
    // Skip the offending document or convert the source file with a full BSON parser first.
    logger.LogWarning(ex, "Skipped a document with an unsupported BSON type during legacy import.");
}

Prevention

When it happens

Trigger: Reading a legacy BSON file that contains an unsupported type code (e.g. 0x06 Undefined, 0x0B Regex, 0x0C DBPointer, 0x0D JavaScript, 0x0E Symbol, 0x0F JavaScriptWScope, 0x11 Timestamp). Encountered during db upgrade/import from external BSON sources.

Common situations: Importing BSON exported from MongoDB or an older LiteDB that used deprecated BSON types; corrupt or truncated data; a custom BSON writer producing non-standard type tags.

Related errors


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