litedb-org/LiteDB · error · Exception

Database not connected

Error message

Database not connected

What it means

Thrown by the LiteDB shell 'show collections' command when no database is open. Listing collections requires querying an open LiteDatabase, so env.Database must be non-null; issuing the command before connecting triggers this guard.

Source

Thrown at LiteDB.Shell/Commands/ShowCollections.cs:20

using System.Linq;

namespace LiteDB.Shell.Commands
{
    [Help(
        Name = "show collections",
        Syntax = "show collections",
        Description = "List all collections inside datafile."
    )]
    internal class ShowCollections : IShellCommand
    {
        public bool IsCommand(StringScanner s)
        {
            return s.Match(@"show\scollections$");
        }

        public void Execute(StringScanner s, Env env)
        {
            if (env.Database == null) throw new Exception("Database not connected");

            var cols = env.Database.GetCollectionNames().OrderBy(x => x).ToArray();

            if (cols.Length > 0)
            {
                env.Display.WriteLine(ConsoleColor.Cyan, string.Join(Environment.NewLine, cols));
            }
        }
    }
}

View on GitHub (pinned to f906a5f850)

Solutions

  1. Open a database first with 'open <filename>' before 'show collections'.
  2. Confirm the connect step reported success; if it errored, fix the connection string/path and retry.
  3. Start the shell with the database path as an argument to auto-connect.

Example fix

// before
> show collections
// after
> open MyData.db
> show collections
Defensive patterns

Strategy: validation

Validate before calling

if (env.Database == null)
{
    env.Display.WriteLine(ConsoleColor.Yellow, "No database open. Run: open <file>");
    return;
}

Type guard

static bool HasDatabase(Env env) => env?.Database != null;

Prevention

When it happens

Trigger: Typing 'show collections' in the LiteDB shell before issuing an 'open'/'connect' command, so env.Database is still null.

Common situations: New shell session with no auto-connect; a failed connect left the database null; running the command against a stream-based or disconnected shell state.

Related errors


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