Skip to content
mbdavid edited this page Mar 14, 2015 · 8 revisions

LiteDB is a simple, fast and ligthweight embedded .NET document database. LiteDB was inpired in MongoDB database and API is very similar to MongoDB C# official driver.

How to install

LiteDB is a serverless database, so there is no install. Just copy LiteDB.dll to your Bin folder and add as Reference. If you prefer, you can use NuGet package: Install-Package LiteDB. If you are running in a web environment, be sure that IIS user has write permission on data folder.

First example

A quick example for store and search documents:

// Create your POCO class entity
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string[] Phones { get; set; }
    public bool IsActive { get; set; }
}

// Open database (or create if not exits)
using(var db = new LiteDatabase(@"C:\Temp\MyData.db"))
{
	// Get a collection (or create, if not exits)
	var col = db.GetCollection<Customer>("customers");

    // Create your new customer instance
	var customer = new Customer
    { 
        Name = "John Doe", 
        Phones = new string[] { "8000-0000", "9000-0000" }, 
        IsActive = true
    };
	
	// Insert new customer document (Id will be auto-increment generate)
	col.Insert(customer);
	
	// Update a document inside a collection
	customer.Name = "Joana Doe";
	
	col.Update(customer);
	
	// Index document using document Name property
	col.EnsureIndex(x => x.Name);
	
	// Use LINQ to query documents
	var results = col.Find(x => x.Name.StartsWith("Jo"));
}

Working with files

Need store files? No problem, use FileStorage.

// Upload a file from file system to database
db.FileStorage.Upload("my-photo-id", @"C:\Temp\picture-01.jpg");

// And download later
db.FileStorage.Download("my-photo-id", @"C:\Temp\copy-of-picture-01.jpg");