How to Pass Parameters to Cosmosasynccontainer
I Am Using the Method queryItems from Cosmosasynccontainer, as Shown Here: (T)Queryitems(Java-Lang-String-Com-Azure-Cosmos-Models-Cosmosqueryrequestoptions-Java...
I am using the method queryItems from CosmosAsyncContainer, as shown here:
(t)queryitems(java-lang-string-com-azure-cosmos-models-cosmosqueryrequestoptions-java-lang-class(t))
So far I have been able to perform a query by concatenating the query String with a parameter, like
String query = QUERY + "\"" + value + "\"";
I would like to avoid this concatenation and pass the parameter in a different way. Maybe by changing the CosmosQueryRequestOptions,
for now I use them like this:
CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
Any help will be appreciated
1 Answer
Instead of creating the query by using string concatenation, you can make use of SqlQuerySpec(String queryText, List<SqlParameter> parameters) which lets you create a parameterized query.
Something like:
SqlQuerySpec spec = new SqlQuerySpec(
"SELECT * FROM c WHERE c.id = @id",
new SqlParameterCollection(new SqlParameter("@id", "id-value")));
You can then use the following override of the queryItems method: <T>queryItems(SqlQuerySpec querySpec, CosmosQueryRequestOptions options, Class<T> classType).