Code Monkey home page Code Monkey logo

litedb's Introduction

LiteDB - A .NET NoSQL Document Store in a single data file

Join the chat at https://gitter.im/mbdavid/LiteDB Build status

LiteDB is a small, fast and lightweight NoSQL embedded database.

  • Serverless NoSQL Document Store
  • Simple API similar to MongoDB
  • 100% C# code for .NET 3.5 / NETStandard 1.4 in a single DLL (less than 200kb)
  • Support for Portable UWP/PCL (thanks to @negue and @szurgot)
  • ACID transactions
  • Data recovery after write failure (journal mode)
  • Datafile encryption using DES (AES) cryptography
  • Map your POCO classes to BsonDocument using attributes or fluent mapper API
  • Store files and stream data (like GridFS in MongoDB)
  • Single data file storage (like SQLite)
  • Index document fields for fast search (up to 16 indexes per collection)
  • LINQ support for queries
  • Shell command line - try this online version
  • Open source and free for everyone - including commercial use
  • Install from NuGet: Install-Package LiteDB

New features in v3

  • Thread Safe - share your LiteDatabase instance across threads
  • MultiKey index - support index in array field
  • Faster than ever - compare results with SQLite here
  • LiteEngine - new simple class to access your documents
  • New checkpoint cache to support large transactions using journal file
  • BsonDocument implements IDictionary, BsonArray implements IList
  • Autocommit disabled support
  • Less 96 bytes per document
  • New: Upsert, FindIndex, Query.Not(Query), Query.Where(Func<>)
  • New BsonMapper class: support Fields and NonPublic members
  • FileStorage now supports OpenWrite("fileId")
  • Virtual index fields -.Index("total", x => x.Products.Sum(p => p.Price))
  • [BsonRefAttribute]
  • Shrink with change password
  • Open datafile in ReadOnly mode
  • LiteDB.Core was removed - LiteDB.dll is now NETStandard 1.4 (Supports UWP, Xamarin, Core, Mono)

Try online

Try LiteDB Web Shell. For security reasons, in the online version not all commands are available. Try the offline version for full feature tests.

Documentation

Visit the Wiki for full documentation

Download

Download the source code or binary only in LiteDB Releases

How to use LiteDB

A quick example for storing and searching documents:

// Create your POCO class
public class Customer
{
    public int Id { get; set; }
    public string Name { 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(@"C:\Temp\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" }, 
        IsActive = 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);
	
	// Index document using a document property
	col.EnsureIndex(x => x.Name);
	
	// Use Linq to query documents
	var results = col.Find(x => x.Name.StartsWith("Jo"));
}

Using fluent mapper and cross document reference for 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; }
	public decimal Total => Products.Sum(p => p.Price);
}        

// Re-use mapper from global instance
var mapper = BsonMapper.Global;

// "Produts" 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
	.Index(x => x.OrderDate)               // Index this field
	.Ignore(x => x.Total);                 // Do not store this
            
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;
		...
	}
                    
}

Where to use?

  • Desktop/local small applications
  • Application file format
  • Small web applications
  • One database per account/user data store
  • Few concurrent write operations

Changelog

Change details for each release are documented in the release notes.

License

MIT

Copyright (c) 2016 - Maurício David

Thanks

A special thanks to @negue and @szurgot helping with portable version.

litedb's People

Contributors

apkd avatar deadlyembrace avatar epsilon-phase avatar eqqon avatar falahati avatar franzalex avatar inosik avatar izackp avatar jlvidal avatar kbdavis07 avatar mbdavid avatar michael-baker avatar michal-novak-pos-digital avatar mstum avatar negue avatar nerai avatar rdstevens avatar sergey-oleynik avatar skysper avatar speedb avatar szurgot avatar tbdbj avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.