Chapter 6. Views and Queries

Once you’ve got your data into Couchbase Server, and you’ve thought about how you might model and represent the different parts of your information, you want to start pulling that information out again. Selecting the information by knowing the document ID will only get you so far.

Looking back at our recipe data, some frequent questions that might be asked are:

  • I’ve got carrots in my fridge, what can I do with them?

  • My bus leaves in 30 minutes, what I can cook in 20 minutes?

Or for the really fussy eaters:

  • I’ve got carrots and 20 minutes, what do I do?

These are not uncommon questions or perspectives on your data, and shouldn’t be unfamiliar concepts to any database user. But how do you translate your requirements from the data that we have stored into the list of recipes that match our selection?

The process that handles that is called the view. One or more views are used to process the stored documents and, in the process, create indexes. The view creates a structure that can be used to query and select information from the bucket by making use of the index structures you define.

Creating Views on Your Data

The principles of map/reduce within Couchbase are very simple. In order to query the documents in your database (rather than just access them by their ID), you have to create a view. The view contains the definition for two functions, map and reduce. The map function is required, and it defines the output format and contents of the view. ...

Get Developing with Couchbase Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.