引言
在.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 類映射到使用屬性或 fluent 映射器 apibsondocument
- · 存儲文件和流數據(如 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 為你的項目提供數據存儲解決方案。