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

Use the latest 4.4.1 as it is more friendly. (i.e. it won’t require __id and __cas for these types of queries)

clone the spring-data-couchbase repository
$ git clone git@github.com:spring-projects/spring-data-couchbase.git

CouchbaseRepositoryQueryIntegrationTests.stringQueryReturnsSimpleType()

@Test
void stringQueryReturnsSimpleType() {
	Airport airport1 = new Airport("1", "myIata1", "MyIcao");
	airportRepository.save(airport1);
	Airport airport2 = new Airport("2", "myIata2__", "MyIcao");
	airportRepository.save(airport2);
	List<String> iatas = airportRepository.getStrings();
	assertEquals(Arrays.asList(airport1.getIata(), airport2.getIata()), iatas);
	List<Long> iataLengths = airportRepository.getLongs();
	assertEquals(Arrays.asList(airport1.getIata().length(), airport2.getIata().length()).toString(),
			iataLengths.toString());

AirportRepository:

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
@Query("SELECT iata, \"\" as __id, 0 as __cas from #{#n1ql.bucket} WHERE #{#n1ql.filter} order by meta().id")
List<String> getStrings();

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
@Query("SELECT length(iata), \"\" as __id, 0 as __cas from #{#n1ql.bucket} WHERE #{#n1ql.filter} order by meta().id")
List<Long> getLongs();