Category : ASP.NET | Date : 1/15/2018 5:41:15 AM | ID : BLOG996020

LiteDB: Embedded .NET NoSQL Database



I was recently in the need for a simple NoSQL database that I could use for an app I was creating for a ASP.NET Core Workshop.

What I was really after was something similar to SQLite.   A single package I could pull into my application that did not require any external service and data stored in a single file.

It didn’t take long to find LiteDB.  After using it for my demo application, I had write a quick post about it.  Just to shed some light for anyone in need.  The project is actually really popular on GitHub, so it seems I might just be late to the party about finding out about it.

LiteDB

LiteDB is serverless database delivered in a single DLL (less than 350kb) fully written in .NET C# managed code (compatible with .NET 3.5, 4.x, NETStandard 1.3 and 2.0).

Install via NuGet or just copy DLL to your bin project folder.

Sample

// Create your POCO class
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string[] Phones { get; set; }
public bool IsActive { get; set; }
}
// Open database (or create if doesn't exist)
using(var db = new LiteDatabase(@"MyData.db"))
{
// Get customer collection
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" },
Age = 39,
IsActive = true
};
// Create unique index in Name field
col.EnsureIndex(x => x.Name, true);
// Insert new customer document (Id will be auto-incremented)
col.Insert(customer);
// Update a document inside a collection
customer.Name = "Joana Doe";
col.Update(customer);
// Use LINQ to query documents (with no index)
var results = col.Find(x => x.Age > 20);
}
view rawlitedb.cs hosted with ? by GitHub

Similar to MongoDB

One thing to note is how the API is similar to the Mongo DB .NET driver.

You also can store files like you can in GridFS.

// Upload a file from file system
db.FileStorage.Upload("$/photos/2014/picture-01.jpg", @"C:\Temp\picture-01.jpg");
// Upload a file from a Stream
db.FileStorage.Upload("$/photos/2014/picture-01.jpg", "picture-01.jpg", stream);
// Find file reference only - returns null if not found
LiteFileInfo file = db.FileStorage.FindById("$/photos/2014/picture-01.jpg");
// Now, load binary data and save to file system
file.SaveAs(@"C:\Temp\new-picture.jpg");
// Or get binary data as Stream and copy to another Stream
file.CopyTo(Response.OutputStream);
// Find all files references in a "directory"
var files = db.FileStorage.Find("$/photos/2014/");
view rawlitedb-files.cs hosted with ? by GitHub

Use Cases

I would think the use cases would be similar in terms of need for something like SQLite.  Because it supports .NET Standard, I would think Xamarin apps would be an excellent fit.

Again, my application was a small local application, which was also ideal.

Are you using LiteDB? Using something different? I’d love to hear what your using and your workloads which you’re using it in.  Let me know on Twitter or in the comments.