Accessing the return value of the reduce function in Couchbase

I am having problems in casting the return value of the built-in reduce function in Couchbase. I am using _count to get the number of documents that are retrieved. I use .reduce() function to the ViewQuery object.

I want the result to be an Integer or long value. I am not able to typecast the final value and I am getting the following exception.

Exception in thread "main" java.lang.ClassCastException: com.couchbase.client.java.view.DefaultViewResult cannot be cast to com.couchbase.client.java.view.ViewRow

ViewQuery query = ViewQuery.from("dev_LCDD", "numDocs").stale(Stale.FALSE).reduce(true);
ViewResult result = theBucket.query(query);
return result;

Upvotes: 0

Views: 85

Answers (1)

Laurent Doguin
Laurent Doguin

Reputation: 528

here's how to retrieve the count result:

    ViewQuery query = ViewQuery.from("dev_LCDD", "count").stale(Stale.FALSE).reduce(true);
    ViewResult result = bucket.query(query);
    List<ViewRow> rows = result.allRows();
    ViewRow row = rows.get(0);
    Integer count = (Integer) row.value();

Upvotes: 1

Related Questions