cosmos db cannot query using IN, spring data native query and array or collection(Java)

Claudio Resende 221 Reputation points
2021-05-10T11:57:52.603+00:00

0

I am trying to create(using spring native query) the findAllId for the reactive repository spring data cosmo DB. Since for the ReactiveCosmosRepository is not implemented.

@Query (value = " SELECT *\n" +
" FROM container_name km\n" +
" WHERE km.id IN (@IDS_test ) \n" +
" ORDER BY km.createdDate DESC ")
Flux<ContainerData> findAllById(@Paramjeet Dahiya ("ids") String[] ids);
or even

@Query (value = " SELECT *\n" +
" FROM container_name km\n" +
" WHERE km.id IN (@IDS_test ) \n" +
" ORDER BY km.createdDate DESC ")
Flux<ContainerData> findAllById(@Paramjeet Dahiya ("ids") Iterable<String> ids);
but it is not retrieving any results, and it is not throwing any exception either.

So the question is, how to use IN operator with spring data native query in cosmos db and collection or array out of the box without having to do a workaround.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,543 questions
Azure Spring Apps
Azure Spring Apps
An Azure platform as a service for running Spring Boot applications at cloud scale. Previously known as Azure Spring Cloud.
116 questions
{count} votes

Accepted answer
  1. Claudio Resende 221 Reputation points
    2021-05-11T09:02:44.137+00:00

    It could be achieved by

    @Query(value = " SELECT *\n" +
    " FROM container_name km\n" +
    " WHERE  array_contains(@ids, km.id, true))\n" +
    " ORDER BY km.createdDate DESC ")
    Flux<ContainerData> findAllById(@Param("ids") Iterable<String> ids);
    
    0 comments No comments

0 additional answers

Sort by: Most helpful