Couchbase Server 4 has landed!

and the Couchbase .NET SDK makes it easy to use!

What's new?

  • N1QL – you can now do rich query against your JSON documents in Couchbase
  • Filtered XDCR – Not all data needs to be copied to a remote data center.
  • LDAP Security – easier managed access to Couchbase server and Log events.
  • Did I mention N1QL?
  • and lot's of other features!

Download and install: Couchbase Server 4.0

N1QL – Rich query

One of the biggest features in Couchbase 4.0 is the option to use N1QL to query your documents. The language is based on SQL and adds a few features to help you work with denormalised JSON documents.

The above N1QL query looks exactly like SQL. The only difference is that in N1QL we are not selecting from a bucket not a table, but this is implementation not syntax. Writing N1QL queries is often just as writing your normal SQL queries.

N1QL is more than SQL

SQL is built for querying structured data with a well defined schema and well defined data types for each column. N1QL on the other hand is for querying JSON data with a flexible schema, therefore we have a few extra options in N1QL to help us query this flexible data model.

It still looks like SQL, but now we have added a few operators to help us work with the JSON data.

Want to know more about N1QL and Couchbase Server?

The Couchbase .NET SDK & N1QL

N1QL has first class support in the .NET SDK and you even have an option to use N1QL with LINQ. Let's look at some of the different ways to use N1QL with the .NET SDK.

The above snippet shows how to use the raw N1QL API, passing in a query and executing it through a call to Query on the Bucket.

But you can also use Query Syntax with LINQ. When using Language Integrated Query (LINQ) .NET uses reflection to build the query string, in our case the N1QL query string. One of the main benefits of using LINQ over handwritten queries is type safety, code completion and compile time check. But to get the type safety LINQ needs a class that defines both the document we query (you can think of this like an in code schema) and the result returned. This can be the same class but can also be two different classes depending on your query.

In this case “MyClass” is used for code completion and building the N1QL query and an anonymous type is used to hold the result.

And of course Lambda Syntax is also an option.

Using N1QL and the .NET SDK in your own project

It's all OpenSource, so you can always grab the source code on GitHub:

If you (like me and most other .NET developers) prefer Nuget packages, you can find them here:

Getting Started with Couchbase Server 4 & .NET

Try-cb-dotnet is the one new samples that I would like to highlight as a great resource for learning and getting familiar with N1QL in .NET try-cb-dotnet

Try-cb-dotnet is split up into a number of branches, each branch represents a step in a tutorial, hopefully making it easier for you to learn how to use N1QL in .net.


Posted by Martin Esmann, Developer Advocate, Couchbase

Martin Esmann is a .Net Developer Advocate at Couchbase. He is a passionate developer with a deep focus on Microsoft Technologies like .NET.

Leave a reply