I’m trying to pull a count from couchbase using spring boot’s Couchbase repo. When I run this query in the couchbase console It works as intended (pulls numbers of attempts within 24 hours of the most recent attempt where the most recent must be within 24 hours of now.)
I dont think the query is the issue, but I think the issue is spring boot doesn’t like executing count queries the same way it pulls entire objects.
public interface AttemptRepository extends CouchbaseRepository<Entity, Long>
{
@Query(
"Select count(*) attempts from {n1ql.bucket} outer where "
Hi @lukel - are there two separate questions? One about “count” and one about "META(default).id? Are you getting an error? If so, what error? Can you show the complete definition of your @Query method? (especially the method signature). What version of spring-data-couchbase are you using?
There is a count query here - notice that the query name begins with ‘count’ and the return type is Long.
@Query("SELECT count(*) FROM #{#n1ql.bucket}")
Long countGood();
I’m only asking about the count query - I only included that META stuff because I saw it as a suggestion in another post, so feel free to ignore it if it doesn’t pertain.
I’m using
using spring-couchbase-5.2.0
That is the whole method signature (it’s actually the entire class minus the imports). Let me copy it straight over (I cannot screenshot it)
public interface AttemptRepository extends CouchbaseRepository<Entity, Long>
{
@Query(
"Select count(*) attempts from {n1ql.bucket} outer where " +
"outer.id = "$1 " +
"and DATE_ADD_STR(outer.createdDate,24,‘hour’) >= " +
"(select RAW MAX(createdDate) lastAttempt " +
"from {n1ql.bucket} inner where " +
"inner.id = "$1 " +
“and inner.createdDate > DATE_ADD_STR(NOW_STR(),-24,‘hour’))[0]”
)
Long countAttempts(String id);
}
When this runs I get a response from the method with value=0 (when I run it in the couchbase console it gives me the proper value). I’ve gotten some errors with couchbase parser and wrong types when tinkering around with it, but couchbase is returning a long equal to zero when I run as is.
inner and outer are reserved words, so that query won’t run in the console nor in spring data couchbase. Also - there is an unmatched quote before both occurences of the $1. No quote is needed there
Can you show the query you are running in the console (are you using it as shown with $1 (if so, what are you using for the parameter), or replacing it with a literal) and the json result?
And can you show the code that calls countAttempts()?
Please post as ‘pre-formatted’ so the quotes etc. don’t get mangled in the html.
I think the issue is spring boot doesn’t like executing count queries the same way it pulls entire objects
You can just use select count(*) from #{#n1ql.bucket} to test that.
Remove the quotes from around $partyId in both places. When you have the quotes, it will match documents with o1.status = ‘REFUSED’ and o1.partyId = ‘$partyId’. You probably want it to match o1.partyId = ‘12345’ or something like that.
Also - in the query editor query, you show two different values for o.partyId and o1.partyId (maybe that’s a typo). If you want to do the same in your spring data query, you’ll need two parameters.
Great. I should have thought of this earlier - but if you enable debug logging for org.springframework.data.couchbase, the query statement sent to couchbase will be logged.
WITH updated AS ((SELECT RAW MAX(o1.updatedOn)
FROM bucket AS o1
WHERE o1.partyId = $partyId
AND o1.status = 'REFUSED'
AND o1.error.code = '5004'
AND o1.createdOn > DATE_ADD_STR(NOW_STR(),-24,'hour'))[0])
SELECT COUNT(1)
FROM bucket AS o
WHERE o.partyId = $partyId
AND o.status = 'REFUSED'
AND o.error.code = '5004'
AND o.createdOn >= DATE_ADD_STR(updated,-24,'hour');
main query. createdOn + x >= static . As there is no index on CreatedOn +x it can’t push . If you rewrite as createdOn >= static - x it can push index if field is CreatedOn