Code it Yourself…

A blog on Microsoft Azure and .NET by Carlos Mendible

Step by step: Couchbase with .Net Core

This week I started to read an understand how Couchbase works and that’s the reason I decided to write: Step by step: Couchbase with .Net Core

Tip: I’ll be using Docker to install and run Couchbase

Now let’s start:

1. Create a folder for your new project

Open a command prompt an run

mkdir couchbase.console
cd couchbase.console

2. Create a console project

dotnet new console

3. Add the Couchbase nuget package

Add the Couchbase nuget package to your project:

dotnet add package CouchbaseNetClient
dotnet restore

4. Replace the contents of Program.cs

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

namespace couchbase.console
    using System;
    using Couchbase;

    class Program
        static void Main(string[] args)
            // Connect to cluster. Defaults to localhost
            using (var cluster = new Cluster())
                // Open the beer sample bucket
                using (var bucket = cluster.OpenBucket("beer-sample"))
                    // Create a new beer document
                    var document = new Document<dynamic>
                        Id = "Polar Ice",
                        Content = new
                            name = "Polar Ice",
                            brewery_id = "Polar"

                    // Insert the beer document
                    var result = bucket.Insert(document);
                    if (result.Success)
                        Console.WriteLine("Inserted document '{0}'", document.Id);

                    // Query the beer sample bucket and find the beer we just added.
                    using (var queryResult = bucket.Query<dynamic>("SELECT name FROM `beer-sample` WHERE brewery_id =\"Polar\""))
                        foreach (var row in queryResult.Rows)

5. Setup Couchbase with Docker

Run the following commands:

docker pull couchbase/server
docker run -d --name db -p 8091-8094:8091-8094 -p 11210:11210 couchbase

Browse to: http://localhost:8091 and setup Couchbase.

Be sure to add the Beer Sample bucket and check the documentation here:

6. Run the program

Run the program and enjoy!

dotnet run

Get the code here:

Hope it helps!