Grouping keys

The real power of composite keys isn't the ability to search in multiple fields, but the ability to perform grouping with aggregation. To illustrate this point, we'll revisit the blog post document with a new property for published date:

{
  "title": "Composite Keys with Couchbase Views ",
  "body": "Composite keys are arrays...",
  "publishedDate": "2014-09-17",
  "type": "post"
}

Let's imagine that we want to display both a list of posts by year and month and the count of posts by year and month. By writing a view that uses compound keys and grouping methods, we are able to achieve both. Starting with the map function, we'll emit the year and month as values in our composite (array) key:

function(doc, meta) { if (doc.type == "post" &&doc.publishedDate) ...

Get Couchbase Essentials 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.