The fundamentals of a view are straightforward. A view creates a perspective on the data stored in your Couchbase buckets in a format that can be used to represent the data in a specific way, define and filter the information, and provide a basis for searching or querying the data in the database based on the content. During the view creation process, you define the output structure, field order, content and any summary or grouping information desired in the view.
As the name suggests, the map function creates a mapping
between the input data (the JSON objects stored in your
database) and the data as you want it displayed in the results
(output) of the view. Every document in the Couchbase bucket
for the view is submitted to the
function in each view once, and it is the output from the
map() function that is used as the result
of the view.
map() function is supplied two
arguments by the views processor. The first argument is the
JSON document data. The optional second argument is the
associated metadata for the document, such as the expiration,
flags, and revision information.
The map function outputs zero or more 'rows' of information
emit() function. Each call to
emit() function is equivalent to a
row of data in the view result. The
emit() function can be called multiple
times within the single pass of the
function. This functionality allows you to create views that
may expose information stored in a compound format within a
single stored JSON record, for example generating a row for
each item in an array.
You can see this in the figure below, where the name, salary and city fields of the stored JSON documents are translated into a table (an array of fields) in the generated view content.
The reduce function is used to summarize the content generated
during the map phase. Reduce functions are optional in a view
and do not have to be defined. When they exist, each row of
output (from each
emit() call in the
map() function) is
processed by the corresponding
If a reduce function is specified in the view definition it is
automatically used. You can access a view without enabling the
reduce function by disabling reduction
reduce=false) when the view is accessed.
Typical uses for a reduce function are to produce a summarized count of the input data, or to provide sum or other calculations on the input data. For example, if the input data included employee and salary data, the reduce function could be used to produce a count of the people in a specific location, or the total of all the salaries for people in those locations.
The combination of the map and the reduce function produce the corresponding view. The two functions work together, with the map producing the initial material based on the content of each JSON document, and the reduce function summarising the information generated during the map phase. The reduction process is selectable at the point of accessing the view, you can choose whether to the reduce the content or not, and, by using an array as the key, you can specifying the grouping of the reduce information.
Each row in the output of a view consists of the view key and the
view value. When accessing a view using only the map function, the
contents of the view key and value are those explicitly stated in
the definition. In this mode the view will also always contain an
id field which contains the document ID of the
source record (i.e. the string used as the ID when storing the
original data record).
When accessing a view employing both the map and reduce functions the key and value are derived from the output of the reduce function based on the input key and group level specified. A document ID is not automatically included because the document ID cannot be determined from reduced data where multiple records may have been merged into one. Examples of the different explicit and implicit values in views will be shown as the details of the two functions are discussed.
You can see an example of the view creation process in the figure below.
Because of the separation of the two elements, you can consider the two functions individually.
For information on how to write map functions, and how the output of the map function affects and supports searching, see Section 9.5.1, “Map Functions”. For details on writing the reduce function, see Section 9.5.2, “Reduce Functions”.
View names must be specified using one or more UTF-8 characters. You cannot have a blank view name. View names cannot have leading or trailing whitespace characters (space, tab, newline, or carriage-return).
To create views, you can use either the Admin Console View editor (see Section 6.5, “Using the Views Editor”), use the REST API for design documents (see Section 9.7, “Design Document REST API”), or use one of the client libraries that support view management.
For more information and examples on how to query and obtain information from a map, see Section 9.8, “Querying Views”.