IgorekPotworek
IgorekPotworek

Reputation: 1335

Counting query in spring-data-couchbase (N1QL)

I'm writing couchbase repository using Spring module and I'm trying to add my own implementation of count method using N1QL query:

public interface MyRepository extends CouchbaseRepository<Entity, Long> {
    @Query("SELECT count(*) FROM default")
    long myCount();
}

But it doesn't work:

org.springframework.data.couchbase.core.CouchbaseQueryExecutionException: Unable to retrieve enough metadata for N1QL to entity mapping, have you selected _ID and _CAS?

So my question is: how can I write counting query using spring-data-couchbase?

I cannot find anything about this in spring documentation. link

Upvotes: 1

Views: 4198

Answers (3)

Sajit Gupta
Sajit Gupta

Reputation: 108

Use this query :

 @Query("SELECT count(*) as count FROM #{#n1ql.bucket} WHERE #{#n1ql.filter} ")
 long myCount();

Upvotes: 1

Can Bezmen
Can Bezmen

Reputation: 122

@Query("SELECT count(*) , META(default).id as _ID, META(default).cas as _CAS FROM default")

Change your query to this one.

Upvotes: 1

Simon Basl&#233;
Simon Basl&#233;

Reputation: 28301

This exception happens because the @Query annotation was designed with the use-case of retrieving entities in mind. Projections to a scalar like count are uncovered corner cases as of RC1. Maybe I can think of some way of adding support for it through explicit boolean flag in the annotation?

Unfortunately I was unable to find a workaround. I was trying to come up with a custom repository method implementation but it appears support for it is broken in 2.0.0-RC1 :(

edit: The use case of simple return types like long, with a SELECT that only uses a single aggregation, should work so this is a bug/improvement. I've opened ticket DATACOUCH-187 in the Spring Data JIRA.

Upvotes: 4

Related Questions