This blog was originally posted on Roi Katz’s personal blog site. Please click here to find the original post.

In the part 1 we’ve built our tiny yet cool application, we’ve even replicated it to another Couchbase Lite.

But what now? We want to actually use it!

So how do we use a database? well at least with “getting” the data you have couple of options:

  1. Get by primary key
  2. Get by an index (or “selecting” it where x)

Up until now, in our simple sample app we could only use the “primary key” to access our data and retrieve it.

But it’s not the only way to get you data from Couchbase Lite.

In this part we will learn the basics of Couchbase Lite indexing. AKA Views.

On the the views, we run our Queries.

So we need to:

  1. Create View
  2. Run Queries on the view
  3. Get the results

We will build our use case – of how using “views” in Couchbase Lite.

  1. Start a new WPF project.
  1. Add Nuget Couchbase.Lite package
  1. Copy that XAML

Which translates to

GUI generates form the XAML above
  1. After you got the basic UI, which you can explore later (nothing much here really), lets go the the actual code.

After we started all up, and Initialized the Database let us define our views.

In this case I’ve defined 1 view – just to show how to set things up.

What you can see here, that once I retrieve a name from the _database I can define a map on it, a map is basically a projection and filtering.

In the example above, I’ve created a view named “docs_by_city”, assigned a delegate, checked if some key (“City”) exist and then emitted it to the index. Simple as that.

We’ve just created our index which for every document contains a property named City – it emits the whole document, you can choose to emit whatever you want, depends on app’s requirements.

It can be adjusted for better performance and smaller index size.

Also you can put as your key about any string you would like or compose your index from several properties to target special needs.

It’s never good to store the entire document in the index as it basically make a copy of the document inside the index. Try to keep your index as small as possible. But if you happen to need some kind of index which has the entire document as a result, for performance it’s better to keep the document in the index instead of accessing the result.Document property – to same some round tripping to the database.

The number “1” here, it the version of the index. During development if you change the map function you also need to increment that number (in case you haven’t deleted the whole database), in order to rebuild the index.

There are 2 special queries.

  1. Get all documents count. (with _database.DocumentCount)
  2. Get all documents. (with _database.CreateAllDocumentsQuery())

After we defined our view (*index) we can start writing the code and use it.

  1. The usage, is fairly simple only 5 steps.
  2. Get the view
  3. Create a query on the view
  4. Define your criteria on the index
  5. Run it
  6. Read it

In code it looks even simpler:

I want the exact “City” so i’ve written on start and end key the same value.

I run the query and check if there is any results.

Then I “beautify” the result (for every value) and return that as a JSON array.

Please pay attention here that I’m not using result.Document but result.Value, as using the result.Document will not use the index and will go and query the database for each result.

So for performance, please use result.key, result.value or result.DocumentId.

Now just add that part to generate some data…

And we are good to go!

This is how we do a simple view!

Of course we have more to come on Couchbase lite views, it’s just the start.

Of course we do need to create the proper properties, so for full project, please check my GitHub page.


Posted by Roi Katz, Solution Architect, Couchbase

Roi is a Couchbase Solution Architect, software developer and architect with over 10 years of broad industry experience. He has been a trainer and author of courses with a specialization in Big Data Systems, NoSQL Databases, Couchbase, Distributed Architecture and Cloud Computing.

One Comment

  1. What is _database here? I am trying to implement the above things in my nodeJs app with couchbase server and I once placed bucket name in place of _database but couldn’t get anything right.

Leave a reply