So far, Couchbase has two different ways to query the contents of a document: using Views or using the N1QL query language (named nickel and in preview state 3 for developers right now).
Views
Couchbase Views creates indexes based on the contents of JSON documents stored in a database and written using the MapReduce programming model . Couchbase uses MapReduce to process documents in a cluster and to create indexes based on their contents. A view is a JavaScript function that runs for each element in a dataset, performs some initial processing and filtering, and then displays the converted result as a key-value set.
The following image shows what the structure looks like:

For example, suppose you have a basket named test and you want to store documents with the following structure:
public class User { [JsonProperty("user_id")] public string UserId { get; set; } [JsonProperty("fname")] public string FirstName { get; set; } [JsonProperty("age")] public string Age { get; set; } [JsonProperty("email")] public string Email { get; set; } [JsonProperty("type")] public string Type { get; set; } }
Now suppose you want to find all users who are 25 years old and you want to know their name and email address. Your opinion may be as follows:
function(doc, meta) { if (doc.type == "user" && doc.age == 25) { emit(doc.user_id, [doc.fname, doc.email]); } }
If you save this view as a development view with the project document name = dev_user and view name = userswith25, you can use this view in your code as follows:
var query = bucket.CreateQuery("dev_user", "userswith25"); var result = bucket.Query<dynamic>(query);
If you want to know more about views, watch this video: Views and Indexing for Couchbase 3.0
N1QL
N1QL (pronounced nickel) is the next generation Couchbases query language. N1QL aims to meet the needs of queries of distributed document-oriented databases. A simple query in N1QL consists of three parts:
- SELECT - parts of the document to return
- FROM - a data basket or data warehouse for working with
- WHERE - Conditions to be met by the document
The query requires only a SELECT . The wildcard * denotes all parts of the document. Queries can return a collection of different structures or fragments of a document. However, they will all meet the conditions in the WHERE clause.
As I said, N1QL is in a preview state, so it is not yet integrated with Couchbase. [EDIT. Integration .NET SDK N1QL is no longer in the alpha version. ] To play with it, you need to download it and integrate with your Couchbase server. Following the previous viewing example, I will show you a query to search for users with the same conditions:
var query = "SELECT fname, email FROM test WHERE type = 'user' and age = 25"; var result = bucket.Query<dynamic>(query);
In parallel with the development of N1QL, Coushbase is developing a Language Integrated Query (LINQ) provider for querying a Couchbase server with N1QL using the Couchbase.NET SDK. This will bring the familiar LINQ syntax for N1QL, and the results will be mapped to POCO. Below I will show an example of how you could use it in the future:
using (var cluster = new Cluster()) { using (var bucket = cluster.OpenBucket("test")) { var users = from c in bucket.Queryable<User>() where c.Age==25 select c; foreach (var user in users) { Console.WriteLine("\tName={0}, Age={1}, Email={2}", user.FirstName, user.Age, user.Email ); } } }
There are also other options:
- Telerik created the Linq provider for Couchbase , which I have not used yet, but I think that it is based on the Couchbase.NET SDK 1.3, and not on the version 2.0 that you are using.
- You can integrate Couchbase with Elasticsearch to provide full-text search in your application using the open source Elasticsearch search engine. With this combination, you can save your documents in Couchbase and search for them later using Elasticsearch. For this you can use this client .sesesesearch.net
Hope this helps.