Glynn Bird

Jul 26, 2018

2 min read

Cloudant Fundamentals: Aggregations

_sum-ming up the basics of Cloudant (part 10 of 10)

It’s been an emotional journey through Cloudant’s fundamentals, but we’re nearly at the end. In this final post, we’ll discuss data aggregation: counting, summing and statistics.

, which we used for and of this series, does not have the ability to perform aggregations, only selection. For example, you can do the equivalent of SELECT * FROM mydb WHERE actor='Al Pacino' but not SELECT COUNT(*) FROM mydb WHERE actor='Al Pacino'.

Organize your view of Cloudant data through JavaScript MapReduce, search faceting, or the cloudant-quickstart library. Photo by on .


One option is to write your own view. A view is defined as a JavaScript function. Cloudant takes each document in the database, passing it to this function in turn, and then storing the emitted key/value pairs in an index on disk.

To calculate counts of documents by actor we would create a map function like this:

This function, when paired with the built-in _count reducer, produces counts of each value of actor. The other built-in reducers (_stats and _sum) can be used to calculate statistics on the second field emitted by your map function:

The above example allows a hierarchical key of year and month to be used to group the aggregation of the orderValuefield — ideal for reports and dashboards.

Facet counts

Another way of calculating simple count aggregations is using the of Cloudant Search. Like MapReduce, Cloudant Search indexes are configured in JavaScript, this time with an index function instead of emit:

The above example indexes two fields (category and price) from our document and instructs both to be "faceted". This means that values can be counted at query time by supplying a counts parameter:

See the for further examples of facet counts and range faceting.

Aggregation with cloudant-quickstart

The simplest way to do aggregation is using the library. Counts are performed without defining the index yourself:

Simply specify the field or array of fields you want to group by, and the library will create the appropriate MapReduce view for you.

The same applies to stats and sum aggregations

The final _count-down

In this Cloudant Fundamentals series we’ve touched on schema design, unique IDs, revision tokens, CRUD operations, querying and aggregation. But there’s a lot more.

There’s a project called that might strike your fancy. You also can for more articles on Cloudant and CouchDB. And of course, you can reach the wider open source community on the or get involved via the . I’ll see you on there!

Things we made with data at IBM’s Center for Open Source Data and AI Technologies.