引言
在 .NET 的世界裡,資料庫選擇至關重要。今天為大家揭曉一款輕量級 NoSQL 資料庫——LiteDB,它小巧但功能強大,為你的專案提供快速、靈活的資料儲存解決方案。無論你是初學者還是資深開發者,LiteDB 都將是你的得力助手!
LiteDB 簡介
LiteDB 是一個開源的、嵌入式 NoSQL 資料庫,完全用 C# 受控程式碼撰寫,專為 .NET 設計。它基於 BSON(Binary JSON)格式儲存資料,支援豐富的查詢操作,且無需安裝和管理複雜的伺服器。LiteDB 非常適合小型專案、桌面應用程式和微服務架構中的資料儲存需求。

LiteDB 功能
- • 簡單的 API,類似於 MongoDB
- • 100% C# 程式碼,用於單一 DLL 中的 .NET 4.5 / NETStandard 1.3/2.0(小於 450kb)
- • 執行緒安全
- • 寫入失敗後的資料復原(WAL 日誌檔案)
- • 使用 DES(AES)加密技術對資料檔案進行加密
- • 將 POCO 類別對應到 BsonDocument,使用屬性或 Fluent 映射器 API
- • 儲存檔案和串流資料(類似於 MongoDB 中的 GridFS)
- • 單一資料檔案儲存(類似於 SQLite)
- • 為文件欄位建立索引以進行快速搜尋
- • 對查詢的 LINQ 支援
- • 用於存取/轉換資料的類 SQL 命令
- • 開源,對所有人免費——包括商業用途
LiteDB 亮點
輕量級:LiteDB 無需安裝伺服器,直接整合到你的 .NET 專案中,佔用空間小,執行速度快。
高效能:支援索引、查詢最佳化和非同步操作,確保資料讀寫的高效率。
易於使用:提供簡潔的 API 和豐富的文件支援,讓你輕鬆上手。
支援 ACID 事務:確保資料的一致性和完整性。
跨平台:LiteDB 可以在 Windows、Linux 和 macOS 等多個平台上執行。
漂亮的 UI 支援:LiteDB Studio——用於資料存取的美觀使用者介面。
如何使用 LiteDB
安裝:透過 NuGet 套件管理員輕鬆安裝 LiteDB。
建立資料庫:在你的專案中建立一個 LiteDatabase 實例,指定資料庫檔案路徑。
定義模型:建立 C# 類別來定義你的資料模型。
儲存和查詢資料:使用 LiteDB 提供的 API 進行資料儲存、查詢和更新操作。
漂亮的 UI
Lite.Studio 用於管理和視覺化資料庫的新 UI

特徵

LiteDB 支援類似 SQL 的語言進行資料和結構操作。你可以使用非常相似的 SQL 關聯式語言來插入、更新、刪除或查詢資料庫。
LINQ 運算式(lambda 函式)可用於在 C# 程式碼中建立流暢的 API 查詢。
新的 LiteDB.Studio 管理工具支援所有 SQL 命令。
還可以從查詢引擎取得詳細的 EXPLAIN PLAN,藉此檢查寫入的查詢是否將以最佳效能執行。
案例實戰
透過一個簡單的實戰案例,展示如何使用 LiteDB 在 .NET 專案中實現資料的新增、刪除、修改、查詢操作。
// 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);
}
更複雜的資料模型使用
// 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;
...
}
}
總結
LiteDB 作為一款輕量級 NoSQL 資料庫,憑藉其小巧、高效能和易於使用的特點,在 .NET 開發領域獲得了廣泛的認可。無論你是初學者還是資深開發者,都可以嘗試使用 LiteDB 為你的專案提供資料儲存解決方案。