Retrieving only one field from document

I’m trying to retrieve only one specific field from a bucket. I tried doing it with @Query annotation like

    @Query("SELECT meta(customer).id as __id, meta(customer).cas as __cas, customer.phoneNumber FROM customer")

But it throws

Unable to make field private final byte java.lang.String.value accessible: module java.base does not “opens java.lang” to unnamed module @1165b38

If there is a better solution to getting only one field from entire bucket, I’m all ears.

Thanks.

Hi @Tunahan_Ayvaz!

When you say 1 field, do you mean 1 field from each document in the bucket or do you mean a singular document?

If you mean the former, your current query appears to select 3 fields (two of which are meta fields) from each document in the customer bucket. The Couchbase Query Tutorial gives a good introduction to N1QL querying in Couchbase. Additionally, I’d recommend testing your query out in the Query Workbench to ensure it works as expected.

If you are trying to fetch a single document, this is best done with a KV lookup instead of a query, but requires that you have the key already.

Hope this helps!

Cheers

Hi @ejscribner, thank you for your answer.

Sorry that my question wasn’t explaining enough. I meant selecting only 1 field from each document. It works just fine when I run it as native query but I couldn’t manage to do so with Spring Data Couchbase.

I’ve been wondering if there is a suggested solution for selecting only one field from each document with Spring Data Couchbase.

Kind regards,

Best

Hi,

Is there any reason you are using query instead of a simple DTO with a Interface called PhoneNumber that only has a getter for the phone number? This is covered in the Spring documentation:

https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.repository.querying

Look at section 5.3.4 DTO Projections.

Thanks
Aaron

1 Like