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.
1 2 3 |
SELECT * FROM bucket WHERE attribute1 = 'HELLO' AND attribute2 LIKE '%N1QL%' LIMIT 2 |
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.
1 2 3 4 5 6 |
SELECT value1, value2, {'yes_this_is_valid': true} FROM bucket WHERE attribute1 = 'HELLO' AND attribute2 LIKE '%N!QL%' AND attribute3 IS NOT MISSING AND ANY item IN list_of.values SATISFIES item.count >= 5 END |
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.
1 2 3 4 5 6 7 8 9 |
string sql = "SELECT COUNT(*) FROM default WHERE type=$param1"; query = new QueryRequest(sql) .AddNamedParameter("param1", "..."); var result = ClusterHelper .GetBucket("travel-sample") .Query(query); return result.Rows; |
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.
1 2 3 4 5 |
// Query Syntax var result = from myItem in ClusterHelper.GetBucket("bucket").Queryable() where myItem.Attribute1 == "...." select new { myValue = myItem.Attribute1} |
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.
1 2 3 4 5 6 |
// Lambda Syntax ClusterHelper .GetBucket("bucket") .Queryable() .Where(item => myItem.Attribute1 == "....") .Select(item => new { myValue = myItem.Attribute1 }; |
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.