Search code examples
springrepositorycouchbasesql++spring-data-couchbase

How can I place a n1ql query with variables in a spring @Query annotation?


I have the following N1QL query:

UPDATE `bucket` 
SET b.terms.min_due.`value` = "12345" FOR b IN balances END 
WHERE entry_id = "12345"

I want to place it in an @Query annotated repository method, but the values for value and entry_id need to be variable.

They are for a patch operation that updates only parts of the document. I have to do it using inline N1ql via @Query, so it is the only thing I've tried.

@Query(UPDATE `bucket` SET b.terms.min_due.`value` = "12345" FOR b IN balances END WHERE entry_id = "12345")
<T> Mono<T> patch(T Aggregate);

I want to construct a repository method that replaces "12345" for value and entry_id with values pulled from the aggregate, then executes the N1ql query, updating only the value specified in the statement instead of the whole couchbase document.


Solution

  • You can use the standard spEL syntax for that:

    @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and companyId = $2 and $1 within #{#n1ql.bucket}")
    BusinessUnity findByAreaRefId(String areaRefId, String companyId);