LiteDB: A NoSQL Database for .NET

Written by on February 14, 2021 in Programming with 0 Comments

I have been looking around for a compact embedded NoSQL database library for .NET, to use as the back-end of my “iLexicon” system. “iLexicon” is written in Lisp and Prolog (I have written a few articles on it before). At present, the entire dictionary component (containing over 300,000 word entries) is resident in memory, and the idea is to move this to a reasonably fast persistent store.

There are many NoSQL databases for .NET. I briefly explored SiaqODB, Ninja Database Pro, UnQLite, and LiteDB and finally chose LiteDB.

Installing LiteDB

Installation is straightforward. Create a project in Visual Studio (I used VS 2019 ver 16.8.5) and use NuGet Package Manager to install the package as described here.

Using Package Manager to Install LiteDB

Using Package Manager to Install LiteDB

This creates a separate sub-directory under the project for the package, and adds the correct reference.

Working with the DB Engine

There are two ways to populate the database. One is to define appropriate classes to model the dictionary entries and use instances of these types to store in the DB. The other is to use “BsonDocument” directly and store the data as “JSON” entries. The latter is schema-less and can be convenient in some cases. I tried both.

Classes to Model Dictionary Entries

Classes to Model Dictionary Entries

Let us create some sample entries from the above:

Instantiating Word Entries

Instantiating Word Entries

We are now ready to insert the data into the database. For each type of entry (basic word, emotion word, pronoun, etc.), we need to create a “collection” first.

Storing the Words

Storing the Words

You can see that I am inserting three words in the category of “pronouns” directly using “BsonDocument” without creating a class, unlike WordDef and EmotionWord.

LiteDB Studio

One additional benefit of using LiteDB is that there is also the “LiteDB Studio” that you can install (actually, build from the sources) separately. It allows you to connect to a local database and manage it using a nice UI.

First we have to connect to the local data store:

Connecting to the DB Using Studio

Connecting to the DB Using Studio

We can double click on the different collections and execute the default query to fetch the data in that collection.

Showing WordDef Entries

Showing WordDef Entries

 

Emotion Words

Emotion Words

 

Pronoun Entries

Pronoun Entries

Overall, I am quite satisfied with LiteDB. It is easy to use, flexible and seems to have acceptable performance. I plan to try it out with the complete iLexicon data in the next couple of weeks, and see how it interfaces with my Prolog NLP parser.

The sample C# source code is here.

Have a nice weekend!

 

Tags: , , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top