LiteDB 是一个小型、快速和轻量级的 .NET NoSQL 嵌入式数据库。

LiteDB 特点
- 无服务器 NoSQL 文档存储
- 简单的 API,类似于 MongoDB
- .NET 4.5 / NETStandard 1.3/2.0 的 100% C# 代码在单个 DLL 中(小于 450kb)
- 线程安全
- 具有完整事务支持的 ACID
- 写入失败后的数据恢复(WAL 日志文件)
- 使用 DES (AES) 加密的数据文件加密
- 将您的 POCO 类映射到
BsonDocument使用属性或流利的映射器 API - 存储文件和流数据(如 MongoDB 中的 GridFS)
- 单一数据文件存储(如 SQLite)
- 用于快速搜索的索引文档字段
- LINQ 对查询的支持
- 用于访问/转换数据的类似 SQL 的命令
- LiteDB Studio - 用于数据访问的漂亮 UI
- 开源且对所有人免费 - 包括商业用途
- 从 NuGet 安装:
Install-Package LiteDB
LiteDB v5 新特性
- 新的存储引擎
- 操作无锁
read(多个读取器) Write每个集合的锁(多个作者)- 内部/系统集合
- 新的
SQL-Like Syntax - 新的查询引擎(支持投影、排序、过滤、查询)
- 部分文档加载(根级别)
如何使用 LiteDB
存储和搜索文档的简单示例:
// 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 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()
.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("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;
...
}
} 在哪里使用?
- 桌面/本地小应用
- 申请文件格式
- 小型网站/应用程序
- 每个帐户/用户数据存储一个数据库
插件
- 一个 GUI 查看器工具:https ://github.com/falahati/LiteDBViewer (v4)
- GUI 编辑器工具:https ://github.com/JosefNemec/LiteDbExplorer (v4)
- Lucene.NET 目录:https ://github.com/sheryever/LiteDBDirectory
- LINQPad 支持:https ://github.com/adospace/litedbpad
- F# 支持:https ://github.com/Zaid-Ajaj/LiteDB.FSharp
- UltraLiteDB(用于 Unity 或 IOT):https ://github.com/rejemy/UltraLiteDB
LiteDB 百科:https://www.modb.pro/wiki/1610
LiteDB 官网:https://www.litedb.org/
LiteDB 开源地址:https://github.com/mbdavid/LiteDB
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




