Lite db for dummies

atosato86 891 views 14 slides Feb 28, 2021
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Embedded NoSQL database for .NET applications.
In this session I explain Blazor demos with experimental LocalDB scenarios


Slide Content

LiteDB for dummies Andrea Tosato

Agenda Data Structure BsonDocument Id & Serialization Object Mapping Collection System File Data

Agenda DbRef ConnectionString Index FileStorage LiteDB Studio Blazor

Data Structure BSON LiteDB stores documents using BSON ( Binary JSON ). BSON is a binary representation of JSON with additional type information. In the documents, the value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents. BSON is a fast and simple way to serialize documents in binary format.

Object Mapping [ BsonCtor ] First, it searches for a constructor with BsonCtorAttribute Then, it searches for a parameterless constructor (and assumes all serialized fields are public and all serialized properties have public setters) Finally, it searches for a constructor whose parameters names match with the names of the fields in the document

Object Mapping Registered Type And many Mapping options

System Collections

System Collections

Import – export from files

BsonDocument LiteDB supports documents up to 16MB after BSON serialization. Document field keys: Keys are case-insensitive Duplicate keys are not allowed LiteDB keeps the original key order, including mapped classes. The only exception is for _id field, which will always be the first field. Document field values: Values can be any BSON value data type: Null, Int32, Int64, Decimal, Double, String, Embedded Document, Array, Binary, ObjectId , Guid , Boolean, DateTime , MinValue , MaxValue When a field is indexed, the value must be less than 256 bytes after BSON serialization. _id field cannot be: Null, MinValue or MaxValue _id is unique indexed field, so value must be less than 256 bytes

DbRef LiteDB is a document database, so there is no JOIN between collections . You can use embedded documents (sub-documents) Create a reference between collections. To create a reference you can use [ BsonRef ] attribute or use the DbRef method from the fluent API mapper.

ConnectionString LiteDB offers 2 types of connections: Direct and Shared . This affect how engine will open data file. Direct : Engine will open the datafile in exclusive mode and will keep it open until Dispose(). The datafile cannot be opened by another process. This is the recommended mode because it’s faster and cachable . Shared : Engine will be close the datafile after each operation. Locks are made using Mutex. This is more expensive, but you can open same file from multiple processes.

Indexes LiteDB improves search performance by using indexes on document fields or expressions. Each index stores the value of a specific expression ordered by the value (and type). Without an index, LiteDB must execute a query using a full document scan . Full document scans are inefficient because LiteDB must deserialize every document in the collection. Indexes in LiteDB are implemented using Skip lists . Insert and search operations have an average complexity of O(log n). This means that in a collection with 1 million documents, a search operation over an indexed expression will take about 13 steps to find the desired document.

Demo https://github.com/andreatosato/LiteDbSamples Docs https://www.litedb.org/docs