简体   繁体   中英

Spring-data-couchbase - running update query

Is any possibility to execute N1QL update query using spring-data?

Ie I have following update query:

"UPDATE USERS USE KEYS $id SET location = $location"

I tried to use couchbaseTemplate.queryN1QL method, but it doesn't work.

Is there any solution of this problem using spring data or even native couchbase java SDK?

the CouchbaseTemplate is more tailored towards Spring Data document's use case, so it expects to run queries that return specific elements and will end up unmarshalled into entities (eg. a User object).

if you want to run a more freeform query, like your update, you can always access the native SDK from the template. In your case:

String paStatement = "UPDATE USERS USE KEYS $id SET location = $location";
JsonObject paramValues = JsonObject.create().put("id", theId).put("location", "theLocation");
N1qlQuery query = N1qlQuery.parametrized(paStatement, paramValues);
template.getCouchbaseBucket().query(query);

I think you can add RETURNING * at the end of your UPDATE statement. It would make the statement returns something. It works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM