One of the very first choices to make when start developing an application is whether to use a SQL or NoSQL Database. Organizing data is a very hard task. When we say organise, we are actually categorising our data according to its type.
NoSQL systems don’t provide the level of data consistency as SQL databases. The fundamental difference between SQL and NoSQL is how data is stored and retrieved.
A conventional database like MySQL, Microsoft SQL Server or Oracle Database uses a schema.
Schema-less or NoSQL data storage is useful in the following scenarios:
There are four common types of NoSQL technologies are available.
In this Article, we’re going to setup & use a document database
LiteDB, which is an open source MongoDB-like database with zero configuration. LiteDB FeaturesI’m going to use the VS Code for creating a project to store & retrieve Data from a NoSQL Database.
Let’s start by creating a console application using dotnet cli
dotnet new console
LiteDB is available at NuGet. you can download from NuGet package manager if using VS or
run this command to install the library from NuGet
using package manager
Install-Package LiteDB
or using .NET CLI
dotnet add package LiteDB
Let’s create a simple User Model for creating User collection & inserting data.
So, create a new File as User.cs
at the root of your Project & put the below Code.
public class User { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } }
Now add the below Code in your Program.cs
File.
using System; using System.Linq; using LiteDB; class Program { static void Main(string[] args) { // Open database (or create if not exits) using(var db = new LiteDatabase(@"MyData.db")) { // Get user collection var users = db.GetCollection<User>("users"); // Create your new user instance var user = new User { Name = "Shehryar Khan", Email = "[email protected]" }; // Insert new user document (Id will be auto-incremented) users.Insert(user); var result = users.Find(x => x.Email == "[email protected]").FirstOrDefault(); Console.WriteLine(result.Name); // Update a document inside a collection user.Name = "S Khan"; users.Update(user); // Index document using a document property users.EnsureIndex(x => x.Name); } } }
In the above Code, first of all we have created a Database as MyData.db
then a User Collection.
We’re using Linq to retrieve data from DB & also Updating a record in our NoSQL DB.
Now Run your Project, you’ll see Name
(Shehryar Khan) in your Console.You can also download & run this example from Github.
There are some tools available to View your LiteDB NoSQL Database Records.
So, Download this LiteDB Manager Tool, unrar the File & run the LiteDBManager.exe
From the File Menu, click on Open Database & choose your MyData.db
File from your Project Root Directory.
It will look like this
You can see your records are stored in the Database.
This is a very basic operation using NoSQL database, you can find some more examples at GitHub.
Comment if you find any difficulty, I’ll love to solve your problem.
Related Articles:
Quick Links
Legal Stuff
Social Media