View creation

View creation

You can create a simple view by defining a JavaScript map function that generates output data, which represents a mapping of the documents stored within the bucket.

For example, here is a simple map function written in JavaScript that returns the document id and the name attribute.
function map( doc, meta )
    {
    if(doc.name != null)
    {
    emit( meta.id, doc.name );
    }
    } 
When you run a query using the view, the emit function generates a list of key-value pairs. The key helps you identify the specific document and the value provides the details you are looking for. Optionally, you can use a reduce function to aggregate the values produced by the map output into a single value (or a single value for each group). With Couchbase Server, you can choose to use a built-in reduce function (such as _sum, _count, and _stats) or write your own custom reduce function.