introduction
In the world of. NET, database selection is crucial. Today, I reveal a lightweight NoSQL database-LiteDB. It is small but powerful, providing a fast and flexible data storage solution for your projects. Whether you are a beginner or an experienced developer, LiteDB will be your right-hand assistant!
Introduction to LiteDB
LiteDB is an open source, embedded NoSQL database written entirely in C#managed code and designed specifically for. NET. It stores data based on the BSON (Binary JSON) format, supports rich query operations, and eliminates the need to install and manage complex servers. LiteDB is ideal for data storage needs in small projects, desktop applications, and microservices architectures.

LiteDB features
- ·Simple API, similar to MongoDB
- ·100% C#code for. NET 4.5/NETStandard 1.3/2.0 in a single DLL (less than 450 kb)
- ·Thread safety
- ·Data recovery after write failure (WAL log file)
- Encrypt data files using DES (AES) encryption technology
- ·Map POCO classes to APIBsonDocument using the property or Fluent mapper
- ·Store file and stream data (such as GridFS in MongoDB)
- ·Individual data file storage (such as SQLite)
- ·Index document fields for quick searches
- ·LINQ support for queries
- ·SQL-like commands used to access/transform data
- ·Open source, free for everyone-including commercial use
LiteDB highlights
-
- Lightweight **: LiteDB does not need to install a server and is directly integrated into your. NET project. It takes up less space and runs quickly.
-
- High performance **: Supports indexing, query optimization and asynchronous operations to ensure efficient data reading and writing.
-
- Easy to use **: Provides concise APIs and rich documentation support to make it easy for you to get started.
-
- Supports ACID transactions **: Ensure data consistency and integrity.
-
- Cross-platform **: LiteDB can run on multiple platforms including Windows, Linux and macOS.
-
- Beautiful UI support **: LiteDB Studio-Beautiful user interface for data access.
How to use LiteDB
-
- Installation **: Easily install LiteDB through NuGet Package Manager.
-
- Create a database **: Create a LiteDatabase instance in your project and specify the database file path.
-
- Define a model **: Create a C#class to define your data model.
-
- Store and query data **: Use the APIs provided by LiteDB for data storage, query and update operations.
Beautiful UI
Lite.Studio's new UI for managing and visualizing databases

features

LiteDB supports SQL-like languages for data and structural manipulation. You can insert, update, delete, or query a database using a very similar SQL relational language.
LINQ expressions (lambda functions) can be used to create smooth API queries in C#code.
The new LiteDB. Studio management tool supports all SQL commands.
You can also obtain a detailed EXPLAIN PLAN from the query engine to check whether written queries will run at optimal performance
Practical case
Through a simple practical case, it shows how to use LiteDB to add, delete, change data in a. NET project.
// 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);
}
Use of more complex data models
// DbRef to cross references
public class Order
{
public ObjectId Id { get; set; }
public DateTime OrderDate { get; set; }
public Address ShippingAddress { get; set; }
public Customer Customer { get; set; }
public List<Product> Products { get; set; }
}
// Re-use mapper from global instance
var mapper = BsonMapper.Global;
// "Products" and "Customer" are from other collections (not embedded document)
mapper.Entity<Order>()
.DbRef(x => x.Customer, "customers") // 1 to 1/0 reference
.DbRef(x => x.Products, "products") // 1 to Many reference
.Field(x => x.ShippingAddress, "addr"); // Embedded sub document
using(var db = new LiteDatabase("MyOrderDatafile.db"))
{
var orders = db.GetCollection<Order>("orders");
// When query Order, includes references
var query = orders
.Include(x => x.Customer)
.Include(x => x.Products) // 1 to many reference
.Find(x => x.OrderDate <= DateTime.Now);
// Each instance of Order will load Customer/Products references
foreach(var order in query)
{
var name = order.Customer.Name;
...
}
}
summary
As a lightweight NoSQL database, LiteDB has been widely recognized in the field of. NET development for its small size, high performance and ease of use. Whether you are a beginner or an experienced developer, you can try using LiteDB to provide a data storage solution for your projects.