一個.net下的輕量級的Serverless 文檔數據庫LiteDB


今天發現了一個.net下的輕量級的Serverless 文檔數據庫LiteDB,感覺還不錯

其主要特點如下:

  • Serverless NoSQL 文檔存儲
  • 類似於 MongoDB 的簡單 API
  • 100% C# 代碼,支持.NET 3.5 / .NET 4.0 / NETStandard 1.3 / NETStandard 2.0,單 DLL (小於 300kb)
  • 支持線程和進程安全
  • 支持文檔/操作級別的 ACID
  • 支持寫失敗后的數據還原 (日志模式)
  • 可使用 DES (AES) 加密算法進行數據文件加密
  • 可使用特性或 fluent 映射 API 將你的 POCO類映射為 BsonDocument
  • 可存儲文件與流數據 (類似 MongoDB 的 GridFS)
  • 單數據文件存儲 (類似 SQLite)
  • 支持基於文檔字段索引的快速搜索 (每個集合支持多達16個索引)
  • 支持 LINQ 查詢
  • Shell 命令行 - 試試這個在線版本
  • 相當快 - 這里是與 SQLite 的對比結果
  • 開源,對所有人免費 - 包括商業應用

簡單的講,它既具有Sqlite的輕便,也具有MongoDB的API友好的特點,作為一個輕量級的數據庫使用是非常方便的。它的應用場景和Sqlite可以 說非常類似,用於桌面,移動環境以及一些小型,輕量級的Web應用上。

由於是單DLL應用,安裝非常簡單,一個Nuget命令就可以搞定: Install-Package LiteDB

官方的示例如下:

class Program
{
    static void Main(string[] args)
    {

        // Open database (or create if doesn't exist)
        using (var db = new LiteDatabase(@"test.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.Nametrue);

            // 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);
        }
    }
}

// Create your POCO class
public class Customer
{
    public int Id { getset; }
    public string Name { getset; }
    public int Age { getset; }
    public string[] Phones { getset; }
    public bool IsActive { getset; }
}

用起來和MongoDB非常類似,如果需要更多幫助可以參看官方的WIKI:https://github.com/mbdavid/LiteDB/wiki。也有人翻譯了中文的版本i: https://github.com/lidanger/LiteDB.wiki_Translation_zh-cn/wiki

其作者在CodeProject上寫了篇文章介紹的更加詳細:LiteDB - A .NET NoSQL Document Store in a single data file。或者也可以參看一些其它人的應用體驗:

我最開始是准備弄一個帶一點檢索功能的日志的,發現已經有人做了這個擴展:NLog.LiteDB ,只需要針對它開發一個查詢功能就可以了。簡單的看了一下它的功能,還是非常好用的,也可以用於圖片之類的小文件存儲,檢索和讀寫的性能應該也還是非常給力的。很多地方感覺已經比Sqlite好用了,准備先小范圍試用一下,如果可以的話准備將其應用到項目中去。

最后說一下其不足的地方,和Sqlite比起來,其第三方GUI工具要少得多,我在Nuget上找了一下,只找到了一個LiteDBViewerLiteDbExplorer。不過有人寫了一個LinqPad的驅動,可能要好用一些。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM