Not long after writing Step by step: Couchbase with .Net Core I discovered Couchbase Lite, which is still in development, but it looks like a great solution for embedded NoSQL scenarios.

So let’s start with this simple: Step by step: Couchbase Lite with .Net Core tutorial.

1. Create a folder for your new project#


Open a command prompt an run

1mkdir couchbase.lite.console
2cd couchbase.lite.console

2. Create a console project#


1dotnet new console

3. Add a nuget.config#


Since Couchbase Lite is still in development you’ll need to add their development nuget server as Nuget source, so create a nuget.config file with the following contents.

1<?xml version="1.0" encoding="utf-8"?>
2<configuration>
3  <packageSources>
4    <add key="Couchbase.Developer" value="http://mobile.nuget.couchbase.com/nuget/Developer/" />
5  </packageSources>
6  <disabledPackageSources />
7</configuration>

4. Add a reference to the Couchbase Lite nuget package#


Run the following command

1dotnet add package couchbase.lite -v 2.0.0-db004
2dotnet restore

5. Replace the contents of Program.cs#


Replace the contents of the Program.cs file with the following code:

 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using Couchbase.Lite;
 5using Couchbase.Lite.Query;
 6
 7namespace couchbase.lite
 8{
 9    class Program
10    {
11        static void Main(string[] args)
12        {
13            // Get current directory to store the database.
14            var dir = Directory.GetCurrentDirectory();
15
16            // Delete the database so we can run the sample without issues.
17            DatabaseFactory.DeleteDatabase("db", dir);
18
19            // Create the default options.
20            var options = DatabaseOptions.Default;
21            options.Directory = dir;
22
23            // Create the database
24            var db = DatabaseFactory.Create("db", options);
25
26            // Create a document the Id == "Polar Ice" an set properties. 
27            var document = db.GetDocument("Polar Ice");
28            document.Properties = new Dictionary<string, object>
29            {
30                ["name"] = "Polar Ice",
31                ["brewery_id"] = "Polar"
32            };
33
34            // Save the document
35            document.Save();
36
37            // Query for the document abd write results to the console.
38            var query = QueryFactory.Select()
39                .From(DataSourceFactory.Database(db))
40                .Where(ExpressionFactory.Property("brewery_id").EqualTo("Polar"));
41
42            var rows = query.Run();
43            foreach (var row in rows)
44            {
45                Console.WriteLine($"Fetched doc with id :: {row.DocumentID}");
46            }
47        }
48    }
49}

6. Run the program#


Run the program

1dotnet run

The console should show the following message:

1Fetched doc with id :: Polar Ice

Get the code here: https://github.com/cmendible/dotnetcore.samples/tree/main/couchbase.lite.console

Hope it helps!