Cloudant Fundamentals: Aggregations

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

Glynn Bird
Jul 26, 2018 · 2 min read

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.

Cloudant Query, which we used for part 8 and part 9 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'.

Image for post
Image for post
Organize your view of Cloudant data through JavaScript MapReduce, search faceting, or the cloudant-quickstart library. Photo by Sven Scheuermeier on Unsplash.


One option is to write your own MapReduce 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 faceting feature 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 documentation for further examples of facet counts and range faceting.

Aggregation with cloudant-quickstart

The simplest way to do aggregation is using the cloudant-quickstart 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 PouchDB that might strike your fancy. You also can search our Medium publication for more articles on Cloudant and CouchDB. And of course, you can reach the wider open source community on the Apache CouchDB chat channels or get involved via the CouchDB mailing lists. I’ll see you on there!

Center for Open Source Data and AI Technologies

Things we made with data at IBM’s Center for Open Source…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store