Deepak Naik
Deepak Naik

Reputation: 53

How to execute dynamic n1ql query with Spring data CouchbaseTemplate

I am migrating spring data couchbase to 4.*

With previous version, I used to execute dynamic N1Ql query like below

N1qlQueryResult result = couchbaseTemplate.queryN1QL(N1qlQuery.simple(complexStringQuery));

Now with upgrade, I see queryN1QL is removed and we are supposed to use findByQuery. But findByQuery does not take string query.

How can I achieve this with newer spring data couchbase version.

Thank You for your help

Upvotes: 1

Views: 732

Answers (2)

gimhanas
gimhanas

Reputation: 97

I have a solution to "findByN1QL" using cluster,

//      return getCouchbaseOperations(repository).findByN1QL(N1qlQuery.simple(query),
//              DetailDocument.class);
        return cluster.query(query).rowsAs(DetailDocument.class);

Does it work or not? Does anyone have any idea about it?

Upvotes: 0

deniswsrosa
deniswsrosa

Reputation: 2460

Because Couchbase 6.6 and 7 added support for scopes and collections, the query was moved to the cluster/bucket level:

@Autowired
private Cluster cluster;

@Autowired
private Bucket bucket;

...

    cluster.query(N1qlQuery.simple("Select * From myBucket",
            N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS))).allRows();

    //or
    
    bucket.query(N1qlQuery.simple("select * from Buckey")).allRows();

Upvotes: 2

Related Questions