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.
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'.
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 (
_sum) can be used to calculate statistics on the second field emitted by your map function:
The above example allows a hierarchical key of
month to be used to group the aggregation of the
orderValuefield — ideal for reports and dashboards.
index function instead of
The above example indexes two fields (
price) from our document and instructs both to be "faceted". This means that values can be counted at query time by supplying a
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
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!