litedb-org/LiteDB · error · InvalidOperationException

The current collection implementation does not support vecto

Error message

The current collection implementation does not support vector operations.

What it means

Thrown by LiteRepository.GetLiteCollection<T> when the collection returned by _db.GetCollection<T> is not the concrete LiteCollection<T>. Vector operations in LiteRepository require the concrete type because they call internal vector APIs; a custom ILiteDatabase that returns a different ILiteCollection implementation cannot satisfy them.

Source

Thrown at LiteDB/Client/Database/LiteRepository.cs:29

    /// <summary>
    /// The LiteDB repository pattern. A simple way to access your documents in a single class with fluent query api
    /// </summary>
    public class LiteRepository : ILiteRepository
    {
        #region Properties

        private readonly ILiteDatabase _db = null;

        private LiteCollection<T> GetLiteCollection<T>(string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);

            if (collection is LiteCollection<T> liteCollection)
            {
                return liteCollection;
            }

            throw new InvalidOperationException("The current collection implementation does not support vector operations.");
        }

        /// <summary>
        /// Get database instance
        /// </summary>
        public ILiteDatabase Database => _db;

        #endregion

        #region Ctor

        /// <summary>
        /// Starts LiteDB database an existing Database instance
        /// </summary>
        public LiteRepository(ILiteDatabase database)
        {
            _db = database;
        }

View on GitHub (pinned to f906a5f850)

Solutions

  1. Construct LiteRepository from a concrete LiteDatabase (string/ConnectionString/Stream) so GetCollection returns LiteCollection<T>.
  2. If you must inject ILiteDatabase, ensure your wrapper delegates GetCollection to the inner LiteDatabase rather than returning its own type.
  3. For tests, use a real in-memory LiteDatabase(new MemoryStream()) instead of a mock.

Example fix

// before
var repo = new LiteRepository(myCustomDb); // myCustomDb is a wrapper
repo.Query<T>().VectorWhereNear("$.emb", vec, 0.5);

// after
using var ms = new MemoryStream();
var repo = new LiteRepository(new LiteDatabase(ms));
repo.Query<T>().VectorWhereNear("$.emb", vec, 0.5);
Defensive patterns

Strategy: validation

Validate before calling

if (_db is not LiteDatabase)
    throw new InvalidOperationException("Vector operations require a concrete LiteDatabase instance.");

Type guard

static bool SupportsVectors(ILiteDatabase db) => db is LiteDatabase;

Prevention

When it happens

Trigger: Using LiteRepository over a custom/test ILiteDatabase whose GetCollection returns a mock or proxy; injecting a wrapped database (e.g. a caching decorator) into LiteRepository(ILiteDatabase); or running vector queries through a unit-test fake.

Common situations: Unit testing with a mocked ILiteDatabase, wrapping LiteDatabase in a decorator for logging/caching, or using a third-party ILiteDatabase implementation that does not derive from LiteCollection.

Related errors


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