Counting Documents Using reduce in Couchebase

In SQL, we do SELECT COUNT(*) to count rows that match a query criteria. In Couchebase, we use the _count reduce. In this article, I will explain two most common use cases for counting documents.

Let us say that, we have documents that look like this:

    type: "Music",
    title: "My country girl",
    tags: ["Country", "Folk", "Rock"]
    type: "Music",
    title: "I like rock and roll",
    tags: ["Rock"]

And let’s say that we need to find out how often a tag appears in all the music documents. For example, “Rock” appears twice above. This is a common requirement. First of all, we need to create a view with a map that indexes each tag. That will be like this:

function (doc, meta) {
    if (doc.type == "Music") {
        var n, i;

        if (doc.tags) {
            n = doc.tags.length;

            //Index every tag
            for (i = 0; i < n; ++i) {
                emit(doc.tags[i], null);

The reduce for this view will be simply _count.

For the two documents we have, the index will look like this:

Key       | Value
"Country" | null
"Folk"    | null
"Rock"    | null
"Rock"    | null

Use Case 1 – Get Count by Key

In this use case, we need to find out how many time a particular tag appears. All we have to do is query the view and supply a tag as key. For example in Java:

Query query = new Query();

query.setReduce(true); //Must be set

When you query a view that has a reduce, the result comes back as the value and not the document. Also, the data type of the value is String. You will need to convert it to Long or Integer. In Java, reading the result will be like this:

ViewResponse result = client.query(view, query);
Iterator<ViewRow> iter = result.iterator();
Integer count = 0;

if (iter.hasNext()) {
    ViewRow row =;

    count = Integer.parseInt(row.getValue()); //Get the value not the document

In this case, count should be 2 for the “Rock” tag.

Use Case 1 – Get Count for All Keys

In this use case, we want to get a complete list of all tags and their appearance counts. This is similar to the GROUP BY clause in SQL. To do this, we simply need to enable grouping in the query. For Java, that will be like this:

Query query = new Query();

query.setGroup(true); //Enable grouping

The query result will look like this:

Key       | Value
"Country" | "1"
"Folk"    | "1"
"Rock"    | "2"

Note: Couchbase can not sort by value of a query. So, if you need to sort the result by count, you will need to do that yourself in the client code.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.