Search:

Search all manuals
Search this manual
Manual
Couchbase Server Manual 2.0
Community Wiki and Resources
Download Couchbase Server 2.0
Couchbase Developer Guide 2.0
Client Libraries
Couchbase Server Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
9.8 Querying Views
Chapter Sections
Chapters

9.8.4. Grouping in Queries

9.8.4.1. Selection when Grouping

If you have specified an array as your compound key within your view, then you can specify the group level to be applied to the query output when using a reduce().

When grouping is enabled, the view output is grouped according to the key array, and you can specify the level within the defined array that the information is grouped by. You do this by specifying the index within the array by which you want the output grouped using the group_level parameter. You can see described in Figure 9.14, “View Grouping”.

Figure 9.14. View Grouping

View Grouping

The group_level parameter specifies the array index (starting at 1) at which you want the grouping occur, and thgenerates a unique value based on this value that is used to identify all the items in the view output that include this unique value:

The grouping will work for any output structure where you have output an compound key using an array as the output value for the key.

9.8.4.1. Selection when Grouping

When using grouping and selection using the key, keys, or startkey/endkey parameters, they query value should match at least the format (and element count) of the group level that is being queried.

For example, using the following map() function to output information by date as an array:

Javascript
function(doc, meta) 
{
  emit([doc.year, doc.mon, doc.day], doc.logtype);
}

If you specify a group_level of 2 then you must specify a key using at least the year and month information. For example, you can specify an explicit key, such as [2012,8]:

?group=true&group_level=2&key=[2012,8]

Or a range:

?group=true&group_level=2&startkey=[2012,2]&endkey=[2012,8]

You can also specify a year, month and day, while still grouping at a higher level. For example, to group by year/month while selecting by specific dates:

?group=true&group_level=2&startkey=[2012,2,15]&endkey=[2012,8,10]

Specifying compound keys that are shorter than the specified group level may output unexpected results due to the selection mechanism and the way startkey and endkey are used to start and stop the selection of output rows.