Dynamic field name using N1QL

Hello !

I’m having a use case here which I can’t seem to solve. Basically, I need to create a webservice where users may query the couchbase cluster “dynamically”. Indeed, i’m storing metadata of different files, and the “creation” of this metadata is up to the user : I don’t have specific fields in my Java POJO, i’m inserting a MAP which gets inserted as a nested object in couchbase.

Now the query I need is pretty simple on paper and looks something like this :
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND $1 = $2") List<FileMetadata> findListMetadata(String pKey, String pValue);

But it doesn’t seem to work, $1 doesn’t seem to ever get replaced by the pKey variable.
I’m using CouchBase 4.5 with the Spring Data connector.

Any ideas on how to solve that use case ?

1 Like

Hello,

I am reposting the answer from Stack Overflow:

You need to dynamically generate the query string, so that pKey is inserted into the query string and pValue is passed as a parameter (as you are doing).