That’s all you need to do in spring-data-couchbase. Just define the method. spring-data-couchbase will write the query based on the query name. If instanceId is the document-id, the findById() method is already defined for you.
@mreiche Thank you for there response. The query that we have associated with the findCustomerByInstanceId(String instanceId) is:
SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.*
FROM as customer UNNEST customer.subscriptions AS subscription
where subscription.instanceId = $1
This seems to fail and this is what we see:
"Received failure from endpoint :query did not project __id. Either use #{n1ql.selectEntity} or project __id and __cas : SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.* FROM as customer UNNEST customer.subscriptions AS subscription WHERE subscription.deleted=false AND subscription.instanceId=$1”
It’s expecting the id and cas to be projected using the names shown in the error message (not _ID and _CAS). In the latest versions it will accept either.
Also - if your Customer entity class results in the document being stored with nested subscriptions, then attempting to retrieve with unnested subscriptions will probably not work out in the way you wish.
The same query works fine when I try it out on the Couchbase server directly. I do see the id and can being populated as well. The Subscriptions also are returned. How come this fails when we send it via the query through the Java code? Appreciate if you can explain in more detail.
com.couchbase.client.core.error.CouchbaseException: query did not project __id. Either use #{n1ql.selectEntity} or project __id and __cas : SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.* FROM as customer UNNEST customer.subscriptions AS subscription WHERE subscription.deleted=false AND subscription.instanceId=$1
at org.springframework.data.couchbase.core.ReactiveFindByQueryOperationSupport$ReactiveFindByQuerySupport.lambda$null$1(ReactiveFindByQueryOperationSupport.java:191) ~[spring-data-couchbase-4.3.3.jar!/:4.3.3]
UNNEST is self JOIN from original document and ARRAY, You are only using ARRAY alias in WHERE clause. Instead of UNNEST use ANY in WHERE clause like below
SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.*
FROM <collection> AS customer
WHERE ANY s IN customer.subscriptions SATISFIES s.deleted=false AND s.instanceId=$1 END;
Remove unwanted subscriptions from projections ARRAY
SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.*,
ARRAY s FOR s IN customer.subscriptions SATISFIES s.deleted=false AND s.instanceId=$1 END AS subscriptions
FROM <collection> AS customer
WHERE ANY s IN customer.subscriptions SATISFIES s.deleted=false AND s.instanceId=$1 END;
Project subscription.* and remove subscriptions (from projection due to customer.*)
SELECT META(`customer` ).id AS _ID, META(`customer` ).cas AS _CAS,
customer.*,
MISSING AS subscriptions,
subscription.*
FROM <collection> AS customer
UNNEST customer.subscriptions AS subscription
WHERE subscription.deleted=false AND subscription.instanceId=$1
Has this query format changed with Couchbase SDK 3.0? The query we had been using was working fine, but once changed the SDK, we have started seeing this issue.
Note, I tried with the new query format you have shared, the number of documents returned is zero.
The earlier query would returned 1 document for the given instanceId.
" SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.* FROM as customer UNNEST customer.subscriptions AS subscription WHERE subscription.deleted=false AND subscription.instanceId=$1”
SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.*
FROM AS customer
WHERE ANY s IN customer.subscriptions SATISFIES s.deleted=false AND s.instanceId=$1 END;
I see the same error:
status: FAILED , reason=query did not project __id. Either use #{n1ql.selectEntity} or project __id and __cas : SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.* FROM scps-prod1._default._default AS customer WHERE ANY s IN customer.subscriptions SATISFIES s.deleted=false AND s.instanceId=$1 END;
com.couchbase.client.core.error.CouchbaseException: query did not project __id. Either use #{n1ql.selectEntity} or project __id and __cas : SELECT META(customer).id AS _ID, META(customer).cas AS _CAS, customer.* FROM AS customer WHERE ANY s IN customer.subscriptions SATISFIES s.deleted=false AND s.instanceId=$1 END;
at reactor.core.publisher.FluxFlatMap$FlatMapMain.onNext(FluxFlatMap.java:386) ~[reactor-core-3.4.16.jar!/:3.4.16]
It’s expecting the id and cas to be projected using the names shown in the error message (not _ID and _CAS).
Right in the error message it says you need to project __id and __cas. In your select statement you have _ID and _CAS. Please fix that.
just change the _ID to __id and _CAS to __cas as indicated in the error message
OR - use a later version of spring-data-couchbase, one of 4.3.10, or any version of 4.4.x, 5.0.x or 5.1.x where the compatibility issue has been fixed.