Using LiteDB in .NET Applications

LiteDB is a lightweight, serverless NoSQL document store writtten entirely in C#. It offers MongoDB-like APIs and is ideal for desktop, mobile, and web applications built with .NET. Unlike relational databases such as SQLite, LiteDB stores data as key-value documents and supports features like ACID transactions, LINQ queries, thread safety, and single-file storage.

Getting Started

Install the LiteDB NuGet package:

Install-Package LiteDB

Define a simple model class:

public class Character
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Basic Operations

Inserting Data

using LiteDB;

// Open or create a database file
using var db = new LiteDatabase("game.db");
var characters = db.GetCollection<Character>("Characters");

// Insert a single document
var hero = new Character { Id = 1, Name = "Thrall" };
characters.Insert(hero);

// Insert multiple documents
var batch = Enumerable.Range(2, 9)
    .Select(i => new Character { Id = i, Name = $"Character #{i}" })
    .ToList();
characters.InsertBulk(batch);

Querying Data

// Retrieve all documents
var all = characters.FindAll().ToList();

// Find one by condition
var thrall = characters.FindOne(x => x.Name == "Thrall");

Deleting Data

var toRemove = characters.FindOne(x => x.Name == "Thrall");
if (toRemove != null)
{
    characters.Delete(toRemove.Id);
}

The database file (game.db) is created in the application’s output directory. For visual inspection, use LiteDB Studio, an official GUI tool for querying and managing LiteDB files.

Tags: LiteDB .NET NoSQL C# Embedded Database

Posted on Mon, 11 May 2026 08:34:03 +0000 by cahamilton